diff --git a/client/src/api/auth.api.ts b/client/src/api/auth.api.ts index ef12635..f839e3d 100644 --- a/client/src/api/auth.api.ts +++ b/client/src/api/auth.api.ts @@ -21,7 +21,7 @@ export const resetPassword = async (data: SignupProps) => { } interface SiginResponse { - data: string; + token: string; } export const signin = async (data: SigninProps) => { diff --git a/client/src/pages/Signin.tsx b/client/src/pages/Signin.tsx index dde0bb6..dbd8dc4 100644 --- a/client/src/pages/Signin.tsx +++ b/client/src/pages/Signin.tsx @@ -22,7 +22,7 @@ const Signin = () => { const onSubmit = (data: SigninProps) => { signin(data).then((res) => { - storeLogin(res.data) + storeLogin(res.token) showAlert('로그인 완료되었습니다.') navigate('/') }, (error) => { diff --git a/server/src/likes/application/add-like.service.ts b/server/src/likes/application/add-like.service.ts index cde9b1c..730f7fb 100644 --- a/server/src/likes/application/add-like.service.ts +++ b/server/src/likes/application/add-like.service.ts @@ -1,10 +1,13 @@ import { StatusCodes } from 'http-status-codes'; import HttpException from 'src/utils/httpException'; +import { validateToken } from 'src/users/jwt/jwt.provider'; + import Like from '../domain/like'; import { save } from '../domain/likes.repository'; -export const addLike = async (userId: number, likedBookId: number): Promise => { +export const addLike = async (accessToken: any, likedBookId: number): Promise => { + const { userId } = validateToken(accessToken); const like = new Like({ userId, likedBookId }); const likeData = like.getDataOfLike(); diff --git a/server/src/likes/web/add-like.controller.ts b/server/src/likes/web/add-like.controller.ts index 2d99870..b8b365a 100644 --- a/server/src/likes/web/add-like.controller.ts +++ b/server/src/likes/web/add-like.controller.ts @@ -5,8 +5,9 @@ import { StatusCodes } from 'http-status-codes'; import { addLike } from '../application/add-like.service'; -const addLikeHandler = ({ params: { id }, body: { userId } }: Request, res: Response) => { - ResponseHandler(() => addLike(userId, Number(id)), StatusCodes.CREATED, res); +const addLikeHandler = ({ params: { id }, headers }: Request, res: Response) => { + const accessToken = headers.authorization; + ResponseHandler(() => addLike(accessToken, Number(id)), StatusCodes.CREATED, res); }; export default addLikeHandler; diff --git a/server/src/users/domain/user.repository.ts b/server/src/users/domain/user.repository.ts index 49ce051..c57b406 100644 --- a/server/src/users/domain/user.repository.ts +++ b/server/src/users/domain/user.repository.ts @@ -42,7 +42,7 @@ export const save = async ({ export const findByEmail = async (email: string): Promise => { const [rows] = await doQuery((connection) => connection.execute( - `SELECT email,password,salt + `SELECT id, email,password,salt FROM users WHERE email = ?`, [email], ), @@ -55,6 +55,7 @@ export const findByEmail = async (email: string): Promise => { } return new User({ + id: row.id, email: row.email, password: row.password, salt: row.salt, diff --git a/server/src/users/jwt/jwt.provider.ts b/server/src/users/jwt/jwt.provider.ts new file mode 100644 index 0000000..8c053a7 --- /dev/null +++ b/server/src/users/jwt/jwt.provider.ts @@ -0,0 +1,31 @@ +import dotenv from 'dotenv'; +import { StatusCodes } from 'http-status-codes'; +import jwt, { type Secret } from 'jsonwebtoken'; +import HttpException from 'src/utils/httpException'; + +dotenv.config(); + +export const generateToken = (loginUser: { userId: number; email: string; password: string }) => { + const payload = { + userId: loginUser.userId, + email: loginUser.email, + }; + + return jwt.sign(payload, process.env.JWT_SECRET as Secret, { + expiresIn: '5m', + }); +}; + +export const validateToken = (token: string) => { + if (token == null || token === undefined || token === '') { + throw new HttpException(`token는 ${token}이 될 수 없습니다`, StatusCodes.BAD_REQUEST); + } + + try { + const decodedJwt = jwt.verify(token, process.env.JWT_SECRET as Secret) as jwt.JwtPayload; + + return decodedJwt; + } catch (e) { + throw new HttpException('인증 할 수 없는 token 입니다', StatusCodes.UNAUTHORIZED); + } +}; diff --git a/server/src/users/signin/application/signin.service.ts b/server/src/users/signin/application/signin.service.ts index 842748d..0fe5374 100644 --- a/server/src/users/signin/application/signin.service.ts +++ b/server/src/users/signin/application/signin.service.ts @@ -1,21 +1,9 @@ -import jwt from 'jsonwebtoken'; - import { StatusCodes } from 'http-status-codes'; import { isMatchPassword } from 'src/users/domain/password.provider'; import { findByEmail } from 'src/users/domain/user.repository'; +import { generateToken } from 'src/users/jwt/jwt.provider'; import HttpException from 'src/utils/httpException'; -const generateToken = (loginUser: { email: string; password: string }) => - jwt.sign( - { - email: loginUser.email, - }, - '1235467898910', - { - expiresIn: '5m', - }, - ); - const signinService = async (email: string, password: string): Promise<{ accessToken: string }> => { const loginUser = await findByEmail(email); if (!loginUser) { @@ -29,6 +17,7 @@ const signinService = async (email: string, password: string): Promise<{ accessT } const token = generateToken({ + userId: userData.id, email: userData.email, password: userData.password, }); diff --git a/server/src/users/signin/web/signin.controller.ts b/server/src/users/signin/web/signin.controller.ts index 5b3251b..bd18b25 100644 --- a/server/src/users/signin/web/signin.controller.ts +++ b/server/src/users/signin/web/signin.controller.ts @@ -14,7 +14,9 @@ const signinController = async (req: Request, res: Response) => { httpOnly: true, }); - return accessToken; + return { + token: accessToken, + }; }; ResponseHandler(signInFunction, StatusCodes.OK, res);