Skip to content

Commit

Permalink
Change all instances of token expiry to something that actually works…
Browse files Browse the repository at this point in the history
… :) (#33)
  • Loading branch information
LachlanCourt authored Jun 22, 2023
1 parent 5ed55e3 commit 2b6bdb5
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/routes/api/v1/accounts/new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const onPost: RequestHandler = async (requestEvent) => {
const token = cryptojs.lib.WordArray.random(32).toString();
// 24 hours
const expiry = new Date(Date.now() + 1000 * 60 * 60 * 24);
console.log(expiry);

await db.token.deleteMany({ where: { email, type: Tokens.ADD_NEW_ACCOUNT } });
await db.token.create({
Expand Down Expand Up @@ -59,7 +58,7 @@ export const onGet: RequestHandler<Response> = async (requestEvent) => {
const tokenData = await db.token.findFirst({ where: { token } });
if (!tokenData) throw error(401, "Invalid Token. Error Code 2");
const { type, expiry, email } = tokenData;
const expired = expiry.getTime() < Math.floor(Date.now() / 1000);
const expired = expiry < new Date();
if (expired) throw error(401, "Invalid Token. Error Code 3");
if (type !== Tokens.ADD_NEW_ACCOUNT)
throw error(401, "Invalid Token. Error Code 4");
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/v1/users/new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const onPost: RequestHandler<Response> = async (requestEvent) => {
const tokenData = await db.token.findFirst({ where: { token } });
if (!tokenData) throw error(401, "Invalid Token. Error Code 2");
const { type, expiry, email } = tokenData;
const expired = expiry.getTime() < Math.floor(Date.now() / 1000);
const expired = expiry < new Date();
if (expired) throw error(401, "Invalid Token. Error Code 3");
if (type !== Tokens.ADD_NEW_USER && type !== Tokens.ADD_NEW_ACCOUNT)
throw error(401, "Invalid Token. Error Code 4");
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/v1/users/reset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const onPost: RequestHandler<Response> = async (requestEvent) => {
const tokenData = await db.token.findFirst({ where: { token } });
if (!tokenData) throw error(401, "Invalid Token. Error Code 2");
const { type, expiry, email } = tokenData;
const expired = expiry.getTime() < Math.floor(Date.now() / 1000);
const expired = expiry < new Date();
if (expired) throw error(401, "Invalid Token. Error Code 3");
if (type !== Tokens.FORGOT_PASSWORD)
throw error(401, "Invalid Token. Error Code 4");
Expand Down
2 changes: 1 addition & 1 deletion src/routes/users/new/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useEndpoint = routeLoader$(async (requestEvent) => {
const tokenData = await db.token.findFirst({ where: { token } });
if (!tokenData) throw error(401, "Invalid Token. Error Code 2");
const { type, expiry } = tokenData;
const expired = expiry.getTime() < Math.floor(Date.now() / 1000);
const expired = expiry < new Date();
if (expired) throw error(401, "Invalid Token. Error Code 3");
if (type !== Tokens.ADD_NEW_USER && type !== Tokens.ADD_NEW_ACCOUNT)
throw error(401, "Invalid Token. Error Code 4");
Expand Down
2 changes: 1 addition & 1 deletion src/routes/users/reset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useEndpoint = routeLoader$(async (requestEvent) => {
const tokenData = await db.token.findFirst({ where: { token } });
if (!tokenData) throw error(401, "Invalid Token. Error Code 2");
const { type, expiry, email } = tokenData;
const expired = expiry.getTime() < Math.floor(Date.now() / 1000);
const expired = expiry < new Date();
if (expired) throw error(401, "Invalid Token. Error Code 3");

if (type !== Tokens.FORGOT_PASSWORD)
Expand Down

0 comments on commit 2b6bdb5

Please sign in to comment.