Skip to content

Commit

Permalink
[#2]Feat: 네이버 로그인 관련 코드 추가
Browse files Browse the repository at this point in the history
로그인 관련하여 논의 필요함, 임시로 만들었
  • Loading branch information
labyrinth30 committed Jun 27, 2024
1 parent a5b580e commit 49d9b50
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/auth/guard/naver-auth-guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class NaverAuthGuard extends AuthGuard('naver') {
constructor() {
super();
}

handleRequest<TUser = any>(
err: any,
user: any,
info: any,
context: ExecutionContext,
status?: any,
): TUser {
//에러가 존재하면 에러 처리로 넘긴다.
if (err || !user) {
throw err;
}
return user;
}
}
44 changes: 44 additions & 0 deletions src/auth/strategy/naver.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-naver-v2';
import { AuthService } from '../auth.service';
import { JwtService } from '@nestjs/jwt';
import {
ENV_NAVER_CALLBACK_URL_KEY,
ENV_NAVER_CLIENT_ID_KEY,
ENV_NAVER_CLIENT_SECRET_KEY,
} from '../../common/const/env-keys.const';

@Injectable()
export class JwtNaverStrategy extends PassportStrategy(Strategy, 'naver') {
constructor(
private authService: AuthService,
private jwtService: JwtService,
private readonly configService: ConfigService,
) {
super({
clientID: configService.get<string>(ENV_NAVER_CLIENT_ID_KEY),
clientSecret: configService.get<string>(ENV_NAVER_CLIENT_SECRET_KEY),
callbackURL: configService.get<string>(ENV_NAVER_CALLBACK_URL_KEY),
});
}

async validate(
accessToken: string,
refreshToken: string,
profile: any,
): Promise<any> {
console.log(profile);
const id = profile.id;
const email = profile.email;
const name = profile.name;
const user = {
id,
email,
name,
};

return user;
}
}
6 changes: 6 additions & 0 deletions src/common/const/env-keys.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ export const ENV_DB_USERNAME_KEY = 'DB_USERNAME';
export const ENV_DB_PASSWORD_KEY = 'DB_PASSWORD';

export const ENV_DB_DATABASE_KEY = 'DB_DATABASE';

export const ENV_NAVER_CLIENT_ID_KEY = 'NAVER_CLIENT_ID';

export const ENV_NAVER_CLIENT_SECRET_KEY = 'NAVER_CLIENT_SECRET';

export const ENV_NAVER_CALLBACK_URL_KEY = 'NAVER_CALLBACK_URL';

0 comments on commit 49d9b50

Please sign in to comment.