Skip to content

Commit 79492aa

Browse files
committed
Merge branch 'main' of github.com:CS3219-AY2425S1/cs3219-ay2425s1-project-g16 into PEER-249-UI-Question-Details
Signed-off-by: SeeuSim <[email protected]>
2 parents fc03b15 + cb19e69 commit 79492aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1425
-4475
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
db-up:
2+
./scripts/ensure-volume.sh
3+
docker-compose --env-file .env.local up -d
4+
5+
db-down:
6+
docker-compose --env-file .env.local down
7+

backend/user/.env.local

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
EXPRESS_ENV="local"
2+
PEERPREP_UI_HOST="http://localhost:5173"
3+
4+
EXPRESS_PORT=9001
25
EXPRESS_DB_HOST="localhost"
36
EXPRESS_DB_PORT=5431
47
POSTGRES_DB="user"

backend/user/docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ services:
2626
args:
2727
# For building with the correct env vars
2828
- env=${EXPRESS_ENV}
29+
- port=${EXPRESS_PORT}
2930
ports:
30-
- "9001:8001"
31+
- "9001:${EXPRESS_PORT}"
3132
command: node dist/index.js
3233
env_file:
3334
- ./.env.local

backend/user/express.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ COPY --from=build --chown=node:node /data/question-express/dist ./dist
1313

1414
ARG env
1515
COPY ".env.${env}" .
16-
EXPOSE 8001
16+
ARG port
17+
EXPOSE ${port}
1718
CMD [ "npm", "run", "start" ]

backend/user/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"description": "",
2121
"dependencies": {
2222
"bcrypt": "^5.1.1",
23+
"cookie-parser": "^1.4.6",
24+
"cors": "^2.8.5",
2325
"drizzle-orm": "^0.33.0",
2426
"env-cmd": "^10.1.0",
2527
"express": "^4.21.0",
@@ -35,6 +37,8 @@
3537
"@swc/core": "^1.7.26",
3638
"@swc/helpers": "^0.5.13",
3739
"@types/bcrypt": "^5.0.2",
40+
"@types/cookie-parser": "^1.4.7",
41+
"@types/cors": "^2.8.17",
3842
"@types/express": "^4.17.21",
3943
"@types/jsonwebtoken": "^9.0.6",
4044
"@types/node": "^22.5.5",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { StatusCodes } from 'http-status-codes';
2+
3+
import { COOKIE_NAME, decodeCookie, isCookieValid } from '@/lib/cookies';
4+
import { IRouteHandler } from '@/types';
5+
import { logger } from '@/lib/utils';
6+
7+
export const checkIsAuthed: IRouteHandler = async (req, res) => {
8+
const cookie: string | undefined = req.cookies[COOKIE_NAME];
9+
if (cookie && isCookieValid(cookie)) {
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+
});
19+
}
20+
return res.status(StatusCodes.UNAUTHORIZED).json('Unauthorised');
21+
};

backend/user/src/controllers/auth/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { StatusCodes } from 'http-status-codes';
22

3+
import { COOKIE_NAME } from '@/lib/cookies';
34
import {
45
loginService,
56
registerService,
@@ -20,13 +21,18 @@ export const login: IRouteHandler = async (req, res) => {
2021
}
2122
return res
2223
.status(StatusCodes.OK)
23-
.cookie('jwtToken', data.cookie, { httpOnly: true })
24+
.cookie(COOKIE_NAME, data.cookie, {
25+
httpOnly: true,
26+
secure: false, // For HTTPS: Set true
27+
sameSite: 'lax',
28+
path: '/',
29+
})
2430
.json(data.user);
2531
};
2632

2733
export const logout: IRouteHandler = async (_req, res) => {
2834
return res
29-
.clearCookie('jwtToken', {
35+
.clearCookie(COOKIE_NAME, {
3036
secure: true,
3137
sameSite: 'none',
3238
})

backend/user/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import app, { dbHealthCheck } from '@/server';
22
import { logger } from '@/lib/utils';
33

4-
const port = process.env.PORT || 8001;
4+
const port = Number.parseInt(process.env.EXPRESS_PORT ?? '8001');
55

66
const listenMessage = `App listening on port: ${port}`;
77
app.listen(port, async () => {

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { JWT_SECRET_KEY } from '@/config';
22
import jwt from 'jsonwebtoken';
33

4+
export const COOKIE_NAME = 'peerprep-user-session';
5+
46
export const generateCookie = <T extends object>(payload: T) => {
57
return jwt.sign(payload, JWT_SECRET_KEY, {
68
expiresIn: '30m',
@@ -13,8 +15,19 @@ export const isCookieValid = (cookie: string) => {
1315
});
1416
};
1517

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

2033
// TODO: Insert proper cookie validity logic and middleware
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import express from 'express';
2+
3+
import { checkIsAuthed } from '@/controllers/auth-check';
4+
5+
const router = express.Router();
6+
7+
router.get('/is-authed', checkIsAuthed);
8+
9+
export default router;

0 commit comments

Comments
 (0)