fix: 로그인 실패 시 홈으로 이동하지 않도록 수정

This commit is contained in:
jhynsoo 2024-08-07 09:21:18 +09:00
parent 01444ea4e4
commit dc67f8d82c
2 changed files with 21 additions and 20 deletions

View File

@ -6,29 +6,24 @@ export function useAuth() {
const setToken = useBoundStore((state) => state.setToken);
const setUserType = useBoundStore((state) => state.setUserType);
const login = (userId, password, onError = () => {}) => {
const login = (userId, password) => {
const formData = {
userId,
password,
};
return instance
.post(`${API_URL}/user/login`, formData)
.then(({ data, config }) => {
const { role: role, 'access-token': accessToken } = data;
config.headers.Authorization = `${accessToken}`;
setToken(accessToken);
return instance.post(`${API_URL}/user/login`, formData).then(({ data, config }) => {
const { role: role, 'access-token': accessToken } = data;
config.headers.Authorization = `${accessToken}`;
setToken(accessToken);
if (role === 'ADMIN') {
setUserType('teacher');
} else if (role === 'STUDENT') {
setUserType('student');
}
})
.catch((e) => {
alert('아이디 또는 비밀번호를 다시 확인해주세요.');
onError(e);
});
if (role === 'ADMIN') {
setUserType('teacher');
} else if (role === 'STUDENT') {
setUserType('student');
}
return accessToken;
});
};
const userRegister = (role, userId, name, email, password, onError = () => {}) => {

View File

@ -22,9 +22,15 @@ export default function LoginPage() {
const id = idRef.current.value;
const password = passwordRef.current.value;
login(id, password).then(() => {
navigate('/', { replace: true });
});
login(id, password)
.then(() => {
navigate('/', { replace: true });
})
.catch(() => {
alert('아이디 또는 비밀번호를 다시 확인해주세요.');
passwordRef.current.value = '';
idRef.current.focus();
});
};
useEffect(() => {