-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from boostcampwm2023/feat/191-kakao-login
[Feat] 카카오 OAuth 로그인 API 구현
- Loading branch information
Showing
7 changed files
with
95 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import "dotenv/config"; | ||
import { BadRequestException, Injectable } from "@nestjs/common"; | ||
import { PassportStrategy } from "@nestjs/passport"; | ||
import { Profile, Strategy } from "passport-kakao"; | ||
import { providerEnum } from "src/utils/enum"; | ||
import { User } from "../users.entity"; | ||
import * as bcrypt from "bcryptjs"; | ||
|
||
@Injectable() | ||
export class KakaoStrategy extends PassportStrategy(Strategy) { | ||
constructor() { | ||
super({ | ||
clientID: process.env.KAKAO_CLIENT_ID, | ||
clientSecret: process.env.KAKAO_CLIENT_SECRET, | ||
callbackURL: `${process.env.FRONTEND_URL}/auth/kakao/callback`, | ||
}); | ||
} | ||
|
||
async validate( | ||
accessToken: string, | ||
refreshToken: string, | ||
profile: Profile, | ||
done: (error: any, user?: any, info?: any) => void, | ||
) { | ||
try { | ||
const { _json } = profile; | ||
const { id, kakao_account, properties } = _json; | ||
|
||
const user = new User(); | ||
|
||
const salt = await bcrypt.genSalt(); | ||
const hashedPassword = await bcrypt.hash(id.toString(), salt); | ||
|
||
user.email = kakao_account.email; | ||
user.userId = id.toString() + "*kakao"; | ||
user.nickname = properties.nickname; | ||
user.password = hashedPassword; | ||
user.provider = providerEnum.KAKAO; | ||
|
||
done(null, user); | ||
} catch (error) { | ||
done( | ||
new BadRequestException( | ||
`카카오 로그인 중 오류가 발생했습니다 : ${error.message}`, | ||
), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters