Skip to content

Commit

Permalink
Merge pull request #173 from jihwooon/issue-99
Browse files Browse the repository at this point in the history
좋아요 기능 개선
  • Loading branch information
jihwooon authored Mar 14, 2024
2 parents 86243a0 + 88a594f commit aa7049c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion client/src/api/auth.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const resetPassword = async (data: SignupProps) => {
}

interface SiginResponse {
data: string;
token: string;
}

export const signin = async (data: SigninProps) => {
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/Signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Signin = () => {

const onSubmit = (data: SigninProps) => {
signin(data).then((res) => {
storeLogin(res.data)
storeLogin(res.token)
showAlert('로그인 완료되었습니다.')
navigate('/')
}, (error) => {
Expand Down
5 changes: 4 additions & 1 deletion server/src/likes/application/add-like.service.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> => {
export const addLike = async (accessToken: any, likedBookId: number): Promise<boolean> => {
const { userId } = validateToken(accessToken);
const like = new Like({ userId, likedBookId });
const likeData = like.getDataOfLike();

Expand Down
5 changes: 3 additions & 2 deletions server/src/likes/web/add-like.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 2 additions & 1 deletion server/src/users/domain/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const save = async ({
export const findByEmail = async (email: string): Promise<User> => {
const [rows] = await doQuery((connection) =>
connection.execute<RowDataPacket[]>(
`SELECT email,password,salt
`SELECT id, email,password,salt
FROM users WHERE email = ?`,
[email],
),
Expand All @@ -55,6 +55,7 @@ export const findByEmail = async (email: string): Promise<User> => {
}

return new User({
id: row.id,
email: row.email,
password: row.password,
salt: row.salt,
Expand Down
31 changes: 31 additions & 0 deletions server/src/users/jwt/jwt.provider.ts
Original file line number Diff line number Diff line change
@@ -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);
}
};
15 changes: 2 additions & 13 deletions server/src/users/signin/application/signin.service.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
});
Expand Down
4 changes: 3 additions & 1 deletion server/src/users/signin/web/signin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const signinController = async (req: Request, res: Response) => {
httpOnly: true,
});

return accessToken;
return {
token: accessToken,
};
};

ResponseHandler(signInFunction, StatusCodes.OK, res);
Expand Down

0 comments on commit aa7049c

Please sign in to comment.