Skip to content

Commit 14bfdd4

Browse files
authored
Merge pull request #14 from CS3219-AY2425S1/PEER-251-Link-Auth-Endpoints
PEER-251 Add auth integrations
2 parents fc3bc9f + 6a36bbf commit 14bfdd4

Some content is hidden

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

47 files changed

+704
-162
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { StatusCodes } from 'http-status-codes';
2+
3+
import { COOKIE_NAME, isCookieValid } from '@/lib/cookies';
4+
import { IRouteHandler } from '@/types';
5+
6+
export const checkIsAuthed: IRouteHandler = async (req, res) => {
7+
const cookie: string | undefined = req.cookies[COOKIE_NAME];
8+
if (cookie && isCookieValid(cookie)) {
9+
return res.status(StatusCodes.OK).json('OK');
10+
}
11+
return res.status(StatusCodes.UNAUTHORIZED).json('Unauthorised');
12+
};

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: 2 additions & 0 deletions
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',
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)