-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
328 additions
and
74 deletions.
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') { | ||
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
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_SECRET } 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_SECRET, { expiresIn: '30m' }) | ||
} | ||
} |
Oops, something went wrong.