Skip to content

Commit

Permalink
➕ add : kakao strategy 추가(#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinddings committed Nov 7, 2024
1 parent a9c95d7 commit 91607bd
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
45 changes: 45 additions & 0 deletions BE/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"mysql2": "^3.11.3",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"passport-kakao": "^1.0.1",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"socket.io": "^4.8.1",
Expand Down
3 changes: 2 additions & 1 deletion BE/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ export class AuthController {

@ApiOperation({ summary: 'Token 인증 테스트 API' })
@Get('/test')
@UseGuards(AuthGuard())
@UseGuards(AuthGuard('jwt'))
test(@Req() req: Request) {
return req;
}

@ApiOperation({ summary: 'Kakao 로그인 API' })
@Get('/kakao')
@UseGuards(AuthGuard('kakao'))
async kakaoLogin(
@Body() authCredentialsDto: AuthCredentialsDto,
@Res() res: Response,
Expand Down
5 changes: 3 additions & 2 deletions BE/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { User } from './user.entity';
import { UserRepository } from './user.repository';
import { JwtStrategy } from './jwt.strategy';
import { JwtStrategy } from './strategy/jwt.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { KakaoStrategy } from './strategy/kakao.strategy';

@Module({
imports: [
Expand All @@ -26,7 +27,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
}),
],
controllers: [AuthController],
providers: [AuthService, UserRepository, JwtStrategy],
providers: [AuthService, UserRepository, JwtStrategy, KakaoStrategy],
exports: [JwtStrategy, PassportModule],
})
export class AuthModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { PassportStrategy } from '@nestjs/passport';
import { InjectRepository } from '@nestjs/typeorm';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UserRepository } from './user.repository';
import { User } from './user.entity';
import { UserRepository } from '../user.repository';
import { User } from '../user.entity';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
Expand Down
32 changes: 32 additions & 0 deletions BE/src/auth/strategy/kakao.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { Profile, Strategy } from 'passport-kakao';

@Injectable()
export class KakaoStrategy extends PassportStrategy(Strategy) {
constructor(private readonly configService: ConfigService) {
super({
clientID: configService.get('KAKAO_CLIENT_ID'),
clientSecret: '',
callbackURL: `${configService.get('BACKEND_URL')}/auth/kakao`,
});
}

async validate(
accessToken: string,
refreshToken: string,
profile: Profile,
done: (error: any, user?: any, info?: any) => void,
) {
try {
const { _json } = profile;
const user = {
kakaoId: _json.id,
};
done(null, user);
} catch (error) {
done(error);
}
}
}

0 comments on commit 91607bd

Please sign in to comment.