๐ ๊ตฌํํ auth ํ์ด์ง ์์งค
const [info, setInfo] = useState({
userId: '',
userPassword: '',
});
const onChangeinfo = e => {
const { name, value } = e.target;
setInfo({ ...info, [name]: value });
};
const passed = info.userId.includes('@') && info.userPassword.length >= 8;
const toAuth = async e => {
const sign = isSelectSignUp ? API.SignUp : API.SignIn;
const config = {
email: info.userId,
password: info.userPassword,
};
e.preventDefault();
try {
const { data } = await axios.post(sign, config);
if (data.access_token) {
localStorage.setItem('token', data.access_token);
alert('๋ก๊ทธ์ธ ์ฑ๊ณต');
navigate('/todo');
}
} catch (err) {
alert('๋ก๊ทธ์ธ์ ์คํจํ์ต๋๋ค');
}
};
useEffect(() => {
if (localStorage.getItem('token')) {
navigate('/todo');
}
}, [navigate]);
๐ ๋ฆฌํฉํ ๋งํ ์ฝ๋!
๋ก๊ทธ์ธ / ํ์ ๊ฐ์ ํ์ด์ง ๊ฐ์ ํ์ด์ง์์ ๊ตฌํํ์ผ๋ฉฐ ์์ ์ปดํฌ๋ํธ๋ก User.js ๋ฅผ ๋์๋ค
const User = () => {
const location = useLocation();
const currentURL = location.pathname;
const isSelectSignUp = currentURL === '/signup';
return (
<Auth
contents={isSelectSignUp ? SIGNUP_DATA : SIGNIN_DATA}
isSelectSignUp={isSelectSignUp}
/>
);
};
export default User;
const SIGNIN_DATA = {
title: '๋ก๊ทธ์ธ',
buttonText: '๋ก๊ทธ์ธํ๊ธฐ',
toGobutton: 'ํ์๊ฐ์
ํ๊ธฐ',
url: '/',
};
const SIGNUP_DATA = {
title: 'ํ์๊ฐ์
',
buttonText: 'ํ์๊ฐ์
ํ๊ธฐ',
toGobutton: '๋ก๊ทธ์ธํ๊ธฐ',
url: '/signup',
};
url๋ฅผ ํตํด isSelectSignUp ์ด true ์ด๋ฉด ํ์๊ฐ์ ์ฐฝ('/signup')์ผ๋ก false๋ฉด ('/')๋ก ํ์๊ฐ์ /๋ก๊ทธ์ธ์ ๋๋์๋ค.
auth ํ์ด์ง๋ ๊ฐ๋จํ๊ฒ ๋ก๊ทธ์ธ/ํ์๊ฐ์ API ๋ ๋ค response๋ก token์ ๋ฐ์์จ๋ค๋ ์ ์ ์ด์ฉํด์ fetch๋ฅผ ํฉ์ณค๋ค.
๊ทธ๋ฆฌ๊ณ auth ํ์ด์ง์ ์ ๊ทผํ์ ๋ ํ ํฐ ์ฌ๋ถ๋ฅผ ํตํด ๋ฐ๋ก '/todo' ๋ก ๊ฐ๋ ๋ก์ง์์ ๋ธ๋ก์ปค๊ฐ ์๊ฒผ๋๋ฐ
๋ธ๋ก์ปค
useEffect(() => {
if (localStorage.getItem('token')) {
navigate('/todo');
}
}, []);
์ด ๋ก์ง๊ณผ
todos.js์
useEffect(() => {
TodoAxios.GET(navigate, setTodos);
}, []);
const access_token = localStorage.getItem('token');
const config = {
headers: { Authorization: 'Bearer ' + access_token },
};
export const TodoAxios = {
GET: async (navigate, setTodos) => {
if (!access_token) {
navigate('/');
} else {
try {
const { data } = await axios.get(API.Todo, config);
setTodos(data);
} catch (err) {
throw new Error(err);
}
}
},
...
์์ ์ถฉ๋์ด ์ผ์ด๋
('/todo') ๋ก ํ์ด์ง ์ด๋์ ํ ์ ์์๋ค :(
TodoAxios.js ์์ access_token์ console ์ฐ์ด๋ณด๋ null ๊ฐ์ด ๋์๋๋ฐ
token์ ๋ฐ๊ธ ๋ฐ๊ธฐ ์ ์ TodoAxios.GET์ด ์ฝํ access_token ๊ฐ์ด null ์ธ ์ฑ๋ก ํจ์๊ฐ ์คํ๋๋ ๋ฏ ํ๋ค
ํด๊ฒฐ
const access_token = () => localstorage.getItem('token')
์ด๋ ์์์ ๋ฌธ์ ์ด๊ธฐ ๋๋ฌธ์ access_token์ ํจ์ํ์ผ๋ก ๋ฐ๊พธ์ด axios ์์์ ์คํ๋๊ฒ ๋ง๋ค์๋ค.
์์ฌ์ด ์ ์ด ์๋ค๋ฉด ์ ์ญ์ ํค๋(access_token)์ ๋ด์ config ๋ณ์๋ axios ์์ ๋ฃ์ด์ผ ํ๋ค๋ ๊ฒ
์ฌ์ฌ์ฉํ๊ธฐ ์ํด config๋ก ์ ์ญ์ ๋บ๋ ๊ฑด๋ฐ ๋ชจ๋ axios์์ ๋ฃ์ด์ฃผ์ด์ผํ๋ค ๐
์ฐพ์๋ณด๋ฉด ๋ ์ข์ ๋ฐฉ๋ฒ์ด ์์ ๊ฑฐ ๊ฐ์๋ฐ
๋ด์ผ ํ๋ฆฌ์จ๋ณด๋ฉ ์์ ์ด ์๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ๊น์ง ํ๋ ๊ฑธ๋ก...!
1์ฃผ์ฐจ ์์ ์ ์ฌ์ ๊ณผ์ ์ฝ๋ ๋ฆฌ๋ทฐ๊ฐ ์๋๋ฐ
์ฝ๋๋ ๋ ์์ฌ์์ด ๋จ์ง๋ง ๊ทธ๋๋ ์ด๋ฒ ๋ฆฌํฉํ ๋ง์ผ๋ก ์ข ๋ ์ข ๋ ์์ฐฌ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ์ ์ ์์ ๊ฑฐ ๊ฐ์ ๋ฟ๋ฏํ๋ค ๐
๊ธฐ๋๋๋ฉด์๋... ๋ฌด์์ด ์ฝ๋๋ฆฌ๋ทฐ
๊ทธ๋๋ ์ฑ์ฅํ๋ ๊ณผ์ ์ด๋ผ ์๊ฐํ๋ฉด ์ฆ๊ฒ๋ค
ํ์ดํ ! ๐ช
:)
๐ ๊ด๋ จ ๊นํ ๋ ํฌ ์ฃผ์
https://github.com/Joeunji0119/wanted-pre-onboarding-frontend
'Wanted-pre-onboarding-7' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ํฐ๋ ํ๋ฆฌ์จ๋ณด๋ฉ ์ฌ์ ๊ณผ์ todos.js ๋ฆฌํฉํ ๋ง (0) | 2022.10.24 |
---|
๋๊ธ