Skip to content

Commit

Permalink
♻️ Refactor: 로그인 성공 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-paik committed Nov 6, 2023
1 parent af6c7c4 commit 60be1ba
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/pages/LoginPage/LoginPage/LoginPage.jsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
import React, { useState } from 'react';
import React from 'react';
import styled from 'styled-components';
import { useNavigate, Link } from 'react-router-dom';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { postUserLogin } from '../../../api/auth';
import { loginAtom } from '../../../atoms/LoginAtom';
import { userInfoAtom } from '../../../atoms/UserAtom';
import LoginForm from '../../../components/Login/LoginForm';

const LoginPage = () => {
const [message, setMessage] = useState('');
const [userInfo, setUserInfo] = useRecoilState(userInfoAtom);
const setLogin = useSetRecoilState(loginAtom);
const navigate = useNavigate();

const handleLogin = async (formData) => {
try {
const res = await postUserLogin(formData);
if (res.status === 422) {
setMessage(res.message);
} else {
setUserInfo({
...userInfo,
account: res.user.accountname,
profileImg: res.user.image,
username: res.user.username,
intro: res.user.intro,
});
setLogin(true);
localStorage.setItem('token', res.user.token);
navigate('/home');
}
} catch (error) {
console.error(error);
const onLogin = (res) => {
if (res.status !== 422) {
setUserInfo({
...userInfo,
account: res.user.accountname,
profileImg: res.user.image,
username: res.user.username,
intro: res.user.intro,
});
setLogin(true);
localStorage.setItem('token', res.user.token);
navigate('/home');
}
};

return (
<Container>
<Title>로그인</Title>
<LoginForm handleLogin={handleLogin} message={message} />
<LoginForm onLogin={onLogin} />
<Signup to="/signup">이메일로 회원가입</Signup>
</Container>
);
Expand Down

0 comments on commit 60be1ba

Please sign in to comment.