-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: oauth2 kakao login #39
Open
joonamin
wants to merge
15
commits into
develop
Choose a base branch
from
FIENMEE-85
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+312
−7
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
085ad56
feat: oauth2 kakao login
joonamin add1210
fix: update package lock file for updating outdated lock file in ci
joonamin 4e7c61c
Update apps/server/src/models/user.ts
joonamin ef25e6b
Update apps/server/src/config/index.ts
joonamin 1507fc7
chore: ci (#37)
laggu be463e8
fix: change query string type for schedule get api (#36)
laggu a52a173
feat: [FE] add React Query client for API state management (#38)
yuchem2 7b388f2
feat: add `validate schedule` middleware (#34)
joonamin e13e3b7
chore: 리뷰 반영
joonamin 95f4224
chore: 리뷰 반영 - 2
joonamin d757fa9
chore: 리뷰 반영
joonamin 42fd86a
chore: 리뷰 반영 - 3
joonamin 7e3f7c6
Merge branch 'develop' into FIENMEE-85
joonamin 7746c91
feat: user model에 provider access token, refresh-token을 저장
joonamin 11d06e1
chore: remove unnecessary module in package.json and rename `loadUser…
joonamin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import enquiries from './enquiries' | ||
import events from './events' | ||
import schedules from './schedules' | ||
import user from './user' | ||
|
||
export default { | ||
enquiries, | ||
events, | ||
schedules, | ||
user, | ||
} |
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 asyncify from 'express-asyncify' | ||
import express, { Request, Response } from 'express' | ||
import { IUserInfo, JWTProvider, OAuthPayload } from '@/utils/auth' | ||
import { User, UserModel } from '@/models/user' | ||
import Kakao from '@/types/provider/kakao' | ||
import { KakaoLoginFailedException } from '@/types/errors' | ||
|
||
const router = asyncify(express.Router()) | ||
|
||
router.post('/login', async (req: Request, res: Response) => { | ||
const oauthPayload: OAuthPayload = req.body | ||
|
||
// todo: refactor this code blocks after introducing new oauth provider | ||
// ==> split the code as service, and adding handler for controller logic | ||
if (oauthPayload.provider === 'KAKAO') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 각 provider 별 필요한 정보를 얻어오는 middleware 와 해당 middleware 에서 얻은 정보로 jwt 를 만드는 handler 로 분리하면 될것 같습니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 express middleware를 활용해서 추후에 작업 진행해볼게요! |
||
const userInfo: IUserInfo = await Kakao.getUserInfo(oauthPayload) | ||
const user: User = await UserModel.findByProviderId(userInfo.providerId) | ||
if (!user) throw new KakaoLoginFailedException(new Error(`not found ${userInfo.providerId}`)) | ||
const jwt = JWTProvider.issueJwt(user) | ||
await UserModel.setProviderCredentials({ | ||
providerId: userInfo.providerId, | ||
providerAccessToken: oauthPayload.accessToken, | ||
providerRefreshToken: oauthPayload.refreshToken, | ||
}) | ||
res.status(200).json({ | ||
accessToken: jwt, | ||
}) | ||
} | ||
}) | ||
|
||
router.post('/register', async (req: Request, res: Response) => { | ||
const oauthRegisterPayload: OAuthPayload = req.body | ||
|
||
if (oauthRegisterPayload.provider === 'KAKAO') { | ||
const registerInfo: IUserInfo = await Kakao.getUserInfo(oauthRegisterPayload) | ||
const user: User = await UserModel.createUser(registerInfo) | ||
const jwt = JWTProvider.issueJwt(user) | ||
await UserModel.setProviderCredentials({ | ||
providerId: registerInfo.providerId, | ||
providerAccessToken: oauthRegisterPayload.accessToken, | ||
providerRefreshToken: oauthRegisterPayload.refreshToken, | ||
}) | ||
res.status(200).json({ | ||
accessToken: jwt, | ||
}) | ||
} | ||
}) | ||
|
||
export default router |
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,25 @@ | ||
import { APIError } from '@/types/errors/error' | ||
|
||
export class OAuthUserInfoException extends APIError { | ||
constructor(cause: Error | string = null) { | ||
super(422, 4220, 'auth http client exception', cause) | ||
Object.setPrototypeOf(this, OAuthUserInfoException.prototype) | ||
Error.captureStackTrace(this, OAuthUserInfoException) | ||
} | ||
} | ||
|
||
export class KakaoLoginFailedException extends APIError { | ||
constructor(cause: Error | string = null) { | ||
super(404, 4040, 'Failed to login with kakao login information', cause) | ||
Object.setPrototypeOf(this, KakaoLoginFailedException.prototype) | ||
Error.captureStackTrace(this, KakaoLoginFailedException) | ||
} | ||
} | ||
|
||
export class KakaoRegisterFailedException extends APIError { | ||
constructor(cause: Error | string = null) { | ||
super(400, 4000, 'Failed to register user information', cause) | ||
Object.setPrototypeOf(this, KakaoRegisterFailedException) | ||
Error.captureStackTrace(this, KakaoRegisterFailedException) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './error' | ||
export * from './server' | ||
export * from './auth' |
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,27 @@ | ||
import { OAuthUserInfoException } from '@/types/errors' | ||
import { OAuthPayload } from '@/utils/auth' | ||
import fetch from 'node-fetch' | ||
|
||
export class Kakao { | ||
public static async getUserInfo(payload: OAuthPayload) { | ||
const response = await fetch('https://kapi.kakao.com/v2/user/me', { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${payload.accessToken}`, | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
}, | ||
}).catch(e => { | ||
throw new OAuthUserInfoException(e) | ||
}) | ||
|
||
const data = await response.json() | ||
|
||
return { | ||
nickname: data.kakao_account.profile.nickname, | ||
provider: 'KAKAO', | ||
providerId: data.id, | ||
} | ||
} | ||
} | ||
|
||
export default Kakao |
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,27 @@ | ||
import { User } from '@/models/user' | ||
import jwt from 'jsonwebtoken' | ||
import { JWT_PRIVATE_KEY } from '@/config' | ||
|
||
export interface OAuthPayload { | ||
accessToken: string | ||
refreshToken: string | ||
provider: string | ||
} | ||
|
||
export interface IUserInfo { | ||
nickname: string | ||
provider: string | ||
providerId: string | ||
} | ||
|
||
export interface IUserCredentials { | ||
providerId: string | ||
providerAccessToken: string | ||
providerRefreshToken: string | ||
} | ||
|
||
export class JWTProvider { | ||
public static issueJwt(user: User): string { | ||
return jwt.sign({ issuer: 'FIENMEE', user: user }, JWT_PRIVATE_KEY, { expiresIn: '30m' }) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private key 는 비대칭키 알고리즘을 의미합니다.
저희는 대칭키로 쓸거니 SECRET 으로 표현하는게 맞을거 같아요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 수정 안됐어요