Skip to content

Commit

Permalink
feat: 카카오 로그인 추가 (#5)
Browse files Browse the repository at this point in the history
* fix: auth module 카카오 로그인 imports

* feat: jwt strategy 추가
  • Loading branch information
Hellol77 authored Sep 17, 2024
1 parent 4cf6850 commit be7f5d3
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/frontend-prod-cd.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Siso - 밸런스 게임 배포 자동화 워크 플로우(prod)
name: Siso - 밸런스 게임 프론트엔드 배포 자동화 워크 플로우(frontend-prod)

on:
push:
Expand Down
Binary file modified backend/.yarn/install-state.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
21 changes: 21 additions & 0 deletions backend/src/auth/strategies/jwt.strategy.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
}
4 changes: 3 additions & 1 deletion backend/src/game/game.controller.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -14,6 +15,7 @@ export class GameController {
return this.gameService.findAll();
}

@UseGuards(AuthGuard('jwt'))
@Post()
async createGame(@Body() createGameDto: CreateGameDto): Promise<Game> {
this.logger.log('Handling create game');
Expand Down
3 changes: 2 additions & 1 deletion backend/src/game/game.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
9 changes: 8 additions & 1 deletion backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
Expand Down
45 changes: 43 additions & 2 deletions backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit be7f5d3

Please sign in to comment.