Skip to content

Commit 5160a37

Browse files
Merge pull request #311 from boostcampwm-2024/dev
[Deploy] 6주차 2차 배포
2 parents 7e9a72a + 781d510 commit 5160a37

File tree

69 files changed

+820
-724
lines changed

Some content is hidden

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

69 files changed

+820
-724
lines changed

backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"cookie-parser": "^1.4.7",
3939
"dotenv": "^16.4.5",
4040
"ioredis": "^5.4.1",
41+
"jsonwebtoken": "^9.0.2",
4142
"mysql2": "^3.11.4",
4243
"nestjs-redis-om": "^0.1.2",
4344
"passport": "^0.7.0",
@@ -59,6 +60,7 @@
5960
"@types/cookie-parser": "^1.4.7",
6061
"@types/express": "^5.0.0",
6162
"@types/jest": "^29.5.2",
63+
"@types/jsonwebtoken": "^9.0.7",
6264
"@types/node": "^20.3.1",
6365
"@types/passport-github": "^1.1.12",
6466
"@types/passport-local": "^1.0.38",

backend/src/app.module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { createDataSource, typeOrmConfig } from "./config/typeorm.config";
1414
import { QuestionListModule } from "./question-list/question-list.module";
1515
import { RedisOmModule } from "@moozeh/nestjs-redis-om";
1616
import { SigServerModule } from "@/signaling-server/sig-server.module";
17-
import { QuestionModule } from './question/question.module';
1817

1918
@Module({
2019
imports: [
@@ -30,7 +29,6 @@ import { QuestionModule } from './question/question.module';
3029
UserModule,
3130
QuestionListModule,
3231
SigServerModule,
33-
QuestionModule,
3432
],
3533
controllers: [AppController],
3634
providers: [AppService],

backend/src/auth/auth.controller.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
import { Controller, Get, Post, Res, UseGuards } from "@nestjs/common";
1+
import {
2+
Controller,
3+
Get,
4+
InternalServerErrorException,
5+
Post,
6+
Res,
7+
UnauthorizedException,
8+
UseGuards,
9+
} from "@nestjs/common";
210
import { AuthGuard } from "@nestjs/passport";
311
import { Response } from "express";
4-
import { AuthService } from "./auth.service";
512
import { setCookieConfig } from "@/config/cookie.config";
613
import { JwtPayload, JwtTokenPair } from "./jwt/jwt.decorator";
714
import { IJwtPayload, IJwtToken, IJwtTokenPair } from "./jwt/jwt.model";
815

916
@Controller("auth")
1017
export class AuthController {
11-
private static ACCESS_TOKEN = "accessToken";
12-
private static REFRESH_TOKEN = "refreshToken";
13-
14-
constructor(private readonly authService: AuthService) {}
18+
constructor() {}
1519

1620
@Post("github")
1721
@UseGuards(AuthGuard("github"))
1822
async githubCallback(
1923
@Res({ passthrough: true }) res: Response,
2024
@JwtTokenPair() pair: IJwtTokenPair
2125
) {
26+
if (!pair) throw new InternalServerErrorException();
2227
return this.setCookie(res, pair.accessToken, pair.refreshToken);
2328
}
2429

2530
@Get("whoami")
2631
@UseGuards(AuthGuard("jwt"))
2732
async handleWhoami(@JwtPayload() payload: IJwtPayload) {
33+
if (!payload) throw new UnauthorizedException();
2834
return payload;
2935
}
3036

@@ -34,12 +40,14 @@ export class AuthController {
3440
@Res({ passthrough: true }) res: Response,
3541
@JwtTokenPair() pair: IJwtTokenPair
3642
) {
43+
if (!pair) throw new UnauthorizedException();
3744
return this.setCookie(res, pair.accessToken);
3845
}
3946

4047
@Post("login")
4148
@UseGuards(AuthGuard("local"))
4249
async login(@Res({ passthrough: true }) res: Response, @JwtTokenPair() pair: IJwtTokenPair) {
50+
if (!pair) throw new UnauthorizedException();
4351
return this.setCookie(res, pair.accessToken, pair.refreshToken);
4452
}
4553

backend/src/auth/jwt/jwt.decorator.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
createParamDecorator,
3-
ExecutionContext,
4-
InternalServerErrorException,
5-
UnauthorizedException,
6-
} from "@nestjs/common";
1+
import { createParamDecorator, ExecutionContext } from "@nestjs/common";
72
import {
83
IJwtPayload as IJwtPayload,
94
IJwtToken as IJwtToken,
@@ -15,7 +10,7 @@ export const JwtPayload = createParamDecorator((data: unknown, ctx: ExecutionCon
1510
const payload = request.user.jwtToken;
1611

1712
if (!isJwtTokenPayload(payload)) {
18-
throw new UnauthorizedException("Invalid jwt token payload");
13+
return null;
1914
}
2015

2116
return payload;
@@ -26,7 +21,7 @@ export const JwtTokenPair = createParamDecorator((data: unknown, ctx: ExecutionC
2621
const payload = request.user.jwtToken;
2722

2823
if (!isJwtTokenPair(payload)) {
29-
throw new InternalServerErrorException("Invalid jwt token");
24+
return null;
3025
}
3126

3227
return payload;
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
import { Injectable } from "@nestjs/common";
22
import { PassportStrategy } from "@nestjs/passport";
3-
import { Strategy } from "passport-jwt";
3+
import { Strategy } from "passport-custom";
44
import { Request } from "express";
5+
import * as jwt from "jsonwebtoken";
56
import "dotenv/config";
67

78
@Injectable()
89
export class AccessTokenStrategy extends PassportStrategy(Strategy, "jwt") {
9-
constructor() {
10-
super({
11-
jwtFromRequest: (req: Request) => {
12-
if (!req || !req.cookies) return null;
13-
return req.cookies["accessToken"];
14-
},
15-
secretOrKey: process.env.JWT_ACCESS_TOKEN_SECRET_KEY,
16-
passReqToCallback: true,
17-
});
18-
}
10+
async validate(req: Request) {
11+
try {
12+
const token = req.cookies?.accessToken;
13+
14+
if (!token) return { jwtToken: null };
1915

20-
async validate(req: Request, payload: any) {
21-
const { userId, username } = payload;
16+
const decoded = jwt.verify(token, process.env.JWT_ACCESS_TOKEN_SECRET_KEY);
17+
const { userId, username } = decoded as any;
2218

23-
return {
24-
jwtToken: {
25-
userId,
26-
username,
27-
},
28-
};
19+
return {
20+
jwtToken: {
21+
userId,
22+
username,
23+
},
24+
};
25+
} catch {
26+
return { jwtToken: null };
27+
}
2928
}
3029
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { QuestionList } from "@/question-list/entity/question-list.entity";
2+
3+
export class QuestionListDto extends QuestionList {
4+
categoryNames?: string[];
5+
}

0 commit comments

Comments
 (0)