diff --git a/.github/workflows/frontend-prod-cd.yml b/.github/workflows/frontend-prod-cd.yml index 855bd6b..be5a97c 100644 --- a/.github/workflows/frontend-prod-cd.yml +++ b/.github/workflows/frontend-prod-cd.yml @@ -1,4 +1,4 @@ -name: Siso - 밸런스 게임 배포 자동화 워크 플로우(prod) +name: Siso - 밸런스 게임 프론트엔드 배포 자동화 워크 플로우(frontend-prod) on: push: diff --git a/backend/.yarn/install-state.gz b/backend/.yarn/install-state.gz index b700d9d..92936cb 100644 Binary files a/backend/.yarn/install-state.gz and b/backend/.yarn/install-state.gz differ diff --git a/backend/package.json b/backend/package.json index 072ee82..28b58df 100644 --- a/backend/package.json +++ b/backend/package.json @@ -28,6 +28,7 @@ "@nestjs/platform-express": "^10.0.0", "cross-env": "^7.0.3", "passport": "^0.7.0", + "passport-jwt": "^4.0.1", "passport-kakao": "^1.0.1", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1" @@ -43,6 +44,7 @@ "@types/jest": "^29.5.2", "@types/node": "^20.3.1", "@types/passport": "^0", + "@types/passport-jwt": "^4", "@types/passport-kakao": "^1", "@types/supertest": "^2.0.12", "@typescript-eslint/eslint-plugin": "^6.0.0", diff --git a/backend/src/auth/auth.module.ts b/backend/src/auth/auth.module.ts index af95569..dcb0c16 100644 --- a/backend/src/auth/auth.module.ts +++ b/backend/src/auth/auth.module.ts @@ -5,10 +5,15 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { User } from 'src/user/user.entity'; import { PassportModule } from '@nestjs/passport'; import { JwtModule } from '@nestjs/jwt'; +import { KakaoStrategy } from 'src/auth/strategies/kakao.strategy'; @Module({ - imports: [TypeOrmModule.forFeature([User]), PassportModule, JwtModule], + imports: [ + TypeOrmModule.forFeature([User]), + PassportModule.register({ defaultStrategy: 'kakao' }), + JwtModule, + ], controllers: [AuthController], - providers: [AuthService], + providers: [AuthService, KakaoStrategy], }) export class AuthModule {} diff --git a/backend/src/auth/strategies/jwt.strategy.ts b/backend/src/auth/strategies/jwt.strategy.ts new file mode 100644 index 0000000..aff06b7 --- /dev/null +++ b/backend/src/auth/strategies/jwt.strategy.ts @@ -0,0 +1,21 @@ +import { Injectable } from '@nestjs/common'; +import { PassportStrategy } from '@nestjs/passport'; +import { Strategy } from 'passport-jwt'; + +@Injectable() +export class JwtStrategy extends PassportStrategy(Strategy) { + constructor() { + super({ + jwtFromRequest: (req) => { + console.log(req.cookies.accessToken); + return req.cookies.accessToken; + }, + ignoreExpiration: false, + secretOrKey: process.env.JWT_SECRET, + }); + } + + async validate(payload) { + return { userId: payload.userId }; + } +} diff --git a/backend/src/game/game.controller.ts b/backend/src/game/game.controller.ts index 6d9b5ef..8879cb7 100644 --- a/backend/src/game/game.controller.ts +++ b/backend/src/game/game.controller.ts @@ -1,7 +1,8 @@ -import { Body, Controller, Get, Logger, Post } from '@nestjs/common'; +import { Body, Controller, Get, Logger, Post, UseGuards } from '@nestjs/common'; import { GameService } from './game.service'; import { Game } from 'src/game/game.entity'; import { CreateGameDto } from 'src/game/dto/create-game.dto'; +import { AuthGuard } from '@nestjs/passport'; @Controller('game') export class GameController { @@ -14,6 +15,7 @@ export class GameController { return this.gameService.findAll(); } + @UseGuards(AuthGuard('jwt')) @Post() async createGame(@Body() createGameDto: CreateGameDto): Promise { this.logger.log('Handling create game'); diff --git a/backend/src/game/game.module.ts b/backend/src/game/game.module.ts index 40bb94e..061214f 100644 --- a/backend/src/game/game.module.ts +++ b/backend/src/game/game.module.ts @@ -5,10 +5,11 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { Game } from 'src/game/game.entity'; import { Item } from 'src/item/item.entity'; import { User } from 'src/user/user.entity'; +import { JwtStrategy } from 'src/auth/strategies/jwt.strategy'; @Module({ imports: [TypeOrmModule.forFeature([Game, User, Item])], controllers: [GameController], - providers: [GameService], + providers: [GameService, JwtStrategy], }) export class GameModule {} diff --git a/backend/src/main.ts b/backend/src/main.ts index 2b1c105..4ce11fc 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -1,11 +1,18 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { Logger } from '@nestjs/common'; +import * as cookieParser from 'cookie-parser'; async function bootstrap() { const logger = new Logger(); - const port = 80; + const port = process.env.PORT || 80; const app = await NestFactory.create(AppModule); + app.enableCors({ + origin: true, + methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', + credentials: true, + }); + app.use(cookieParser()); await app.listen(port); logger.log(`Application is running on: ${port}`); } diff --git a/backend/yarn.lock b/backend/yarn.lock index d959625..b5666da 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -1384,6 +1384,15 @@ __metadata: languageName: node linkType: hard +"@types/jsonwebtoken@npm:*": + version: 9.0.6 + resolution: "@types/jsonwebtoken@npm:9.0.6" + dependencies: + "@types/node": "npm:*" + checksum: 10c0/9c29e3896e5fb6056e54d87514643e59e0cfb966ae25171a107776270195bba955f0373e98c8ed6450c145b18984f5df9cf0fcac360f382cec3c7c4d3510b202 + languageName: node + linkType: hard + "@types/jsonwebtoken@npm:9.0.5": version: 9.0.5 resolution: "@types/jsonwebtoken@npm:9.0.5" @@ -1425,6 +1434,16 @@ __metadata: languageName: node linkType: hard +"@types/passport-jwt@npm:^4": + version: 4.0.1 + resolution: "@types/passport-jwt@npm:4.0.1" + dependencies: + "@types/jsonwebtoken": "npm:*" + "@types/passport-strategy": "npm:*" + checksum: 10c0/0ced0eaa7bb379d674821108d9bc6758223f1a5f2b9790ec78d3eaaccce6a58a424cf8ed22b53d813740ec53d929e21d92cf794ef0fb30c732866750763c0d7a + languageName: node + linkType: hard + "@types/passport-kakao@npm:^1": version: 1.0.3 resolution: "@types/passport-kakao@npm:1.0.3" @@ -1435,6 +1454,16 @@ __metadata: languageName: node linkType: hard +"@types/passport-strategy@npm:*": + version: 0.2.38 + resolution: "@types/passport-strategy@npm:0.2.38" + dependencies: + "@types/express": "npm:*" + "@types/passport": "npm:*" + checksum: 10c0/d7d2b1782a0845bd8914250aa9213a23c8d9c2225db46d854b77f2bf0129a789f46d4a5e9ad336eca277fc7e0a051c0a2942da5c864e7c6710763f102d9d4295 + languageName: node + linkType: hard + "@types/passport@npm:*": version: 1.0.16 resolution: "@types/passport@npm:1.0.16" @@ -2236,6 +2265,7 @@ __metadata: "@types/jest": "npm:^29.5.2" "@types/node": "npm:^20.3.1" "@types/passport": "npm:^0" + "@types/passport-jwt": "npm:^4" "@types/passport-kakao": "npm:^1" "@types/supertest": "npm:^2.0.12" "@typescript-eslint/eslint-plugin": "npm:^6.0.0" @@ -2249,6 +2279,7 @@ __metadata: jest: "npm:^29.5.0" mysql2: "npm:^3.11.0" passport: "npm:^0.7.0" + passport-jwt: "npm:^4.0.1" passport-kakao: "npm:^1.0.1" prettier: "npm:^3.0.0" reflect-metadata: "npm:^0.1.13" @@ -5072,7 +5103,7 @@ __metadata: languageName: node linkType: hard -"jsonwebtoken@npm:9.0.2": +"jsonwebtoken@npm:9.0.2, jsonwebtoken@npm:^9.0.0": version: 9.0.2 resolution: "jsonwebtoken@npm:9.0.2" dependencies: @@ -5993,6 +6024,16 @@ __metadata: languageName: node linkType: hard +"passport-jwt@npm:^4.0.1": + version: 4.0.1 + resolution: "passport-jwt@npm:4.0.1" + dependencies: + jsonwebtoken: "npm:^9.0.0" + passport-strategy: "npm:^1.0.0" + checksum: 10c0/d7e2b472d399f596a1db31310f8e63d10777ab7468b9a378c964156e5f0a772598b007417356ead578cfdaf60dc2bba39a55f0033ca865186fdb2a2b198e2e7e + languageName: node + linkType: hard + "passport-kakao@npm:^1.0.1": version: 1.0.1 resolution: "passport-kakao@npm:1.0.1" @@ -6014,7 +6055,7 @@ __metadata: languageName: node linkType: hard -"passport-strategy@npm:1.x.x": +"passport-strategy@npm:1.x.x, passport-strategy@npm:^1.0.0": version: 1.0.0 resolution: "passport-strategy@npm:1.0.0" checksum: 10c0/cf4cd32e1bf2538a239651581292fbb91ccc83973cde47089f00d2014c24bed63d3e65af21da8ddef649a8896e089eb9c3ac9ca639f36c797654ae9ee4ed65e1