Skip to content

Commit cb19e69

Browse files
authored
Merge pull request #18 from CS3219-AY2425S1/PEER-251-Link-Auth-Endpoints
PEER-251 Add ExpiresAt claim
2 parents 14bfdd4 + 01307c5 commit cb19e69

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { StatusCodes } from 'http-status-codes';
22

3-
import { COOKIE_NAME, isCookieValid } from '@/lib/cookies';
3+
import { COOKIE_NAME, decodeCookie, isCookieValid } from '@/lib/cookies';
44
import { IRouteHandler } from '@/types';
5+
import { logger } from '@/lib/utils';
56

67
export const checkIsAuthed: IRouteHandler = async (req, res) => {
78
const cookie: string | undefined = req.cookies[COOKIE_NAME];
89
if (cookie && isCookieValid(cookie)) {
9-
return res.status(StatusCodes.OK).json('OK');
10+
const decoded = decodeCookie(cookie);
11+
const expireTimeInMillis = decoded.exp * 1000;
12+
logger.info(
13+
'[/auth-check/check-is-authed]: Expires At ' + new Date(expireTimeInMillis).toLocaleString()
14+
);
15+
return res.status(StatusCodes.OK).json({
16+
message: 'OK',
17+
expiresAt: expireTimeInMillis,
18+
});
1019
}
1120
return res.status(StatusCodes.UNAUTHORIZED).json('Unauthorised');
1221
};

backend/user/src/lib/cookies/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@ export const isCookieValid = (cookie: string) => {
1515
});
1616
};
1717

18+
export type CookiePayload = {
19+
id: string;
20+
};
21+
22+
type CookieType<T> = T & {
23+
iat: number;
24+
exp: number;
25+
};
26+
1827
export const decodeCookie = (cookie: string) => {
19-
return jwt.decode(cookie);
28+
const decoded = jwt.decode(cookie) as CookieType<CookiePayload>;
29+
30+
return decoded;
2031
};
2132

2233
// TODO: Insert proper cookie validity logic and middleware

backend/user/src/services/auth/login.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { StatusCodes } from 'http-status-codes';
33

44
import { db, users } from '@/lib/db';
55
import type { ILoginPayload, ILoginResponse } from './types';
6-
import { generateCookie } from '@/lib/cookies';
6+
import { CookiePayload, generateCookie } from '@/lib/cookies';
77
import { getIsPasswordValid } from '@/lib/passwords';
88

99
const _FAILED_ATTEMPTS_ALLOWED = 3;
@@ -74,7 +74,7 @@ export const loginService = async (payload: ILoginPayload): Promise<ILoginRespon
7474
unlockTime: null,
7575
});
7676
}
77-
const jwtToken = generateCookie({ id: user.id });
77+
const jwtToken = generateCookie<CookiePayload>({ id: user.id });
7878
return {
7979
code: StatusCodes.OK,
8080
data: {

frontend/src/components/blocks/route-guard.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export const loader =
2727
queryFn: async () => {
2828
return await checkIsAuthed();
2929
},
30-
staleTime: (_query) => {
31-
return 1000 * 60 * 30;
30+
staleTime: ({ state: { data } }) => {
31+
const now = new Date();
32+
const expiresAt = data?.expiresAt ?? now;
33+
return Math.max(expiresAt.getTime() - now.getTime(), 0);
3234
},
3335
}),
3436
authedRoute: authedRoutes.includes(path),
@@ -43,7 +45,7 @@ export const RouteGuard = () => {
4345
<Await resolve={data}>
4446
{({ isAuthed, authedRoute, path }) => {
4547
usePageTitle(path);
46-
return isAuthed ? (
48+
return isAuthed.isAuthed ? (
4749
authedRoute ? (
4850
// Route is authed and user is authed - proceed
4951
<Outlet />

frontend/src/lib/query-client.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { QueryClient } from '@tanstack/react-query';
22

3-
const ONE_SECOND_IN_MILLIS = 1000;
3+
// const ONE_SECOND_IN_MILLIS = 1000;
44
export const queryClient = new QueryClient({
5-
defaultOptions: {
6-
queries: {
7-
staleTime: 10 * ONE_SECOND_IN_MILLIS,
8-
},
9-
},
5+
// defaultOptions: {
6+
// queries: {
7+
// staleTime: 10 * ONE_SECOND_IN_MILLIS,
8+
// },
9+
// },
1010
});

frontend/src/services/user-service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,18 @@ export const checkIsAuthed = (param?: { signal: AbortSignal }) => {
3333
.catch((err) => {
3434
if (err !== null) {
3535
console.error(err);
36+
return { status: HttpStatusCode.Unauthorized, data: undefined };
3637
}
3738
})
38-
.then((response) => {
39-
return response?.status === HttpStatusCode.Ok;
39+
.then(async (response) => {
40+
if (response && response.status < 400) {
41+
return {
42+
isAuthed: true,
43+
expiresAt: response.data ? new Date(response.data.expiresAt) : new Date(),
44+
};
45+
}
46+
return {
47+
isAuthed: false,
48+
};
4049
});
4150
};

0 commit comments

Comments
 (0)