Skip to content

Commit 0983370

Browse files
authored
Merge pull request #258 from boostcampwm-2024/fix/login
[FE] 로그인 에러 처리
2 parents 1068fb8 + 7bd51ea commit 0983370

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

FE/src/components/Login/index.tsx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,30 @@ import { FormEvent, useEffect, useState } from 'react';
55
import { login } from 'service/auth';
66
import useAuthStore from 'store/useAuthStore.ts';
77
import Overay from '../ModalOveray.tsx';
8+
import Toast from 'components/Toast.tsx';
89

910
export default function Login() {
1011
const { isOpen, toggleModal } = useLoginModalStore();
1112
const [email, setEmail] = useState('');
1213
const [password, setPassword] = useState('');
1314
const { setIsLogin } = useAuthStore();
14-
const [errorCode, setErrorCode] = useState<number>(200);
1515

1616
useEffect(() => {
17-
setEmail('');
18-
setPassword('');
17+
setEmail('jindding');
18+
setPassword('1234');
1919
}, [isOpen]);
2020

2121
if (!isOpen) return;
2222

2323
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
2424
e.preventDefault();
2525
const res = await login(email, password);
26+
if ('message' in res) {
27+
let message = '';
28+
if (res.statusCode === 401) message = '존재하지 않는 사용자입니다.';
29+
else if (res.statusCode === 400) message = '잘못된 입력형식입니다.';
2630

27-
if ('error' in res) {
28-
setErrorCode(res.statusCode);
31+
Toast({ message, type: 'error' });
2932
return;
3033
}
3134

@@ -40,8 +43,12 @@ export default function Login() {
4043
import.meta.env.VITE_TEST_PW,
4144
);
4245

43-
if ('error' in res) {
44-
setErrorCode(res.statusCode);
46+
if ('message' in res) {
47+
let message = '';
48+
if (res.statusCode === 401) message = '존재하지 않는 사용자입니다.';
49+
else if (res.statusCode === 400) message = '잘못된 입력형식입니다.';
50+
51+
Toast({ message, type: 'error' });
4552
return;
4653
}
4754

@@ -58,17 +65,9 @@ export default function Login() {
5865
<>
5966
<Overay onClick={() => toggleModal()} />
6067
<section className='fixed left-1/2 top-1/2 flex w-[500px] -translate-x-1/2 -translate-y-1/2 flex-col rounded-2xl bg-white p-20 shadow-lg'>
61-
<h2 className='text-3xl font-bold'>JuGa</h2>
62-
<p className='my-3 h-5 text-sm font-semibold text-juga-red-60'>
63-
{
64-
{
65-
'401': '존재하지 않는 사용자입니다.',
66-
'400': '잘못된 입력형식입니다.',
67-
}[errorCode]
68-
}
69-
</p>
70-
<form className='mb-2 flex flex-col' onSubmit={handleSubmit}>
71-
<div className='my-10 flex flex-col gap-2'>
68+
<h2 className='mb-5 text-3xl font-bold'>JuGa</h2>
69+
<form className='flex flex-col mb-2' onSubmit={handleSubmit}>
70+
<div className='flex flex-col gap-2 my-10'>
7271
<Input
7372
type='text'
7473
placeholder='아이디'
@@ -84,7 +83,7 @@ export default function Login() {
8483
autoComplete='current-password'
8584
/>
8685
</div>
87-
<button className='rounded-3xl bg-juga-blue-40 py-2 text-white transition hover:bg-juga-blue-50'>
86+
<button className='py-2 text-white transition rounded-3xl bg-juga-blue-40 hover:bg-juga-blue-50'>
8887
로그인
8988
</button>
9089
</form>

FE/src/store/useAuthStore.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import { create } from 'zustand';
22

33
type AuthStore = {
4-
accessToken: string | null;
54
isLogin: boolean;
6-
setAccessToken: (token: string) => void;
75
setIsLogin: (isLogin: boolean) => void;
8-
resetToken: () => void;
96
};
107

118
const useAuthStore = create<AuthStore>((set) => ({
12-
accessToken: null,
139
isLogin: false,
1410
setIsLogin: (isLogin: boolean) => {
1511
set({ isLogin });
1612
},
17-
setAccessToken: (token: string) => {
18-
set({ accessToken: token, isLogin: token !== null });
19-
},
20-
resetToken: () => {
21-
set({ accessToken: null, isLogin: false });
22-
},
2313
}));
2414

2515
export default useAuthStore;

FE/src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ export type LoginSuccessResponse = {
33
};
44

55
export type LoginFailResponse = {
6-
error: string;
7-
message: string[];
6+
message: string;
87
statusCode: number;
8+
timestamp: string;
9+
path: string;
910
};
1011

1112
export type TiemCategory = 'D' | 'W' | 'M' | 'Y';

0 commit comments

Comments
 (0)