Skip to content

Commit

Permalink
Merge pull request #63 from boostcampwm-2024/be/feature/kakao_oauth
Browse files Browse the repository at this point in the history
[BE/feature] kakao 로그인 구현
  • Loading branch information
HBLEEEEE authored Nov 19, 2024
2 parents 57f1d1c + c24101e commit e13a946
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
17 changes: 16 additions & 1 deletion apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { signUpResponseDecorator } from './decorator/signup.decorator';
import { loginResponseDecorator } from './decorator/login.decorator';
import { AuthGuard } from '@nestjs/passport';
import { GoogleLoginDto } from './dto/googleLogin.dto';
import { KakaoLoginDto } from './dto/kakaoLogin.dto';
import { oauthResponseDecorator } from './decorator/oauth.decorator';

@Controller('api/auth')
Expand Down Expand Up @@ -37,10 +38,24 @@ export class AuthController {

@Get('google/redirect')
@UseGuards(AuthGuard('google'))
@ApiOperation({ summary: '구글 로그인 리다이렉션 API' })
@oauthResponseDecorator()
@ApiOperation({ summary: '구글 로그인 후 리다이렉션 API' })
async googleRedirect(@Req() googleLoginDto: GoogleLoginDto) {
const tokens = await this.authService.googleLogin(googleLoginDto);
return successhandler(successMessage.LOGIN_SUCCESS, tokens);
}

@Get('kakao')
@UseGuards(AuthGuard('kakao'))
@ApiOperation({ summary: '카카오 로그인 API' })
async kakaoLogin() {}

@Get('kakao/redirect')
@UseGuards(AuthGuard('kakao'))
@ApiOperation({ summary: '카카오 로그인 리다이렉션 API' })
@oauthResponseDecorator()
async kakaoRedirect(@Req() kakaoLoginDto: KakaoLoginDto) {
const tokens = await this.authService.kakaoLogin(kakaoLoginDto);
return successhandler(successMessage.LOGIN_SUCCESS, tokens);
}
}
3 changes: 2 additions & 1 deletion apps/backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { PassportModule } from '@nestjs/passport';
import { GoogleStrategy } from './strategies/google.strategy';
import { KakaoStrategy } from './strategies/kakao.strategy';

@Module({
imports: [
Expand All @@ -21,7 +22,7 @@ import { GoogleStrategy } from './strategies/google.strategy';
})
})
],
providers: [AuthService, GoogleStrategy],
providers: [AuthService, GoogleStrategy, KakaoStrategy],
controllers: [AuthController]
})
export class AuthModule {}
17 changes: 14 additions & 3 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { authQueries } from './auth.queries';
import { LoginDto } from './dto/login.dto';
import { JwtService } from '@nestjs/jwt';
import { GoogleLoginDto } from './dto/googleLogin.dto';
import { KakaoLoginDto } from './dto/kakaoLogin.dto';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -69,12 +70,12 @@ export class AuthService {
return { accessToken, refreshToken };
}

async googleLogin(googleLoginDto: GoogleLoginDto) {
const { email, name } = googleLoginDto;
async loginWithSocialMedia(email: string, nickname: string) {
const existingUser = await this.databaseService.query(authQueries.findByEmailQuery, [email]);

if (!existingUser) {
const hashedPassword = await bcrypt.hash('default', 10);
await this.databaseService.query(authQueries.signUpQuery, [email, hashedPassword, name]);
await this.databaseService.query(authQueries.signUpQuery, [email, hashedPassword, nickname]);
}
const member = await this.databaseService.query(authQueries.findByEmailQuery, [email]);
const payload = {
Expand All @@ -86,4 +87,14 @@ export class AuthService {
const refreshToken = this.jwtService.sign(payload, { expiresIn: '7d' });
return { accessToken, refreshToken };
}

async googleLogin(googleLoginDto: GoogleLoginDto) {
const { email, name } = googleLoginDto;
return this.loginWithSocialMedia(email, name);
}

async kakaoLogin(kakaoLoginDto: KakaoLoginDto) {
const { email, nickname } = kakaoLoginDto;
return this.loginWithSocialMedia(email, nickname);
}
}
4 changes: 4 additions & 0 deletions apps/backend/src/auth/dto/kakaoLogin.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class KakaoLoginDto {
email: string;
nickname: string;
}
28 changes: 28 additions & 0 deletions apps/backend/src/auth/strategies/kakao.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-kakao';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class KakaoStrategy extends PassportStrategy(Strategy, 'kakao') {
constructor(private configService: ConfigService) {
super({
clientID: configService.get<string>('KAKAO_REST_API_KEY'),
callbackURL: configService.get<string>(
'KAKAO_CALLBACK_URL',
'http://localhost:3000/auth/kakao/redirect'
)
});
}

async validate(
accessToken: string,
refreshToken: string,
profile: { nickname: string; email: string }
) {
const { nickname, email } = profile;

const member = { email, nickname };
return member;
}
}

0 comments on commit e13a946

Please sign in to comment.