๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Wanted-pre-onboarding-7

์›ํ‹ฐ๋“œ ํ”„๋ฆฌ์˜จ๋ณด๋”ฉ ์‚ฌ์ „๊ณผ์ œ auth.js ๋ฆฌํŒฉํ† ๋ง

by ์€์ง€:) 2022. 10. 24.
728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿ‘† ๊ตฌํ˜„ํ•œ 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

 

GitHub - Joeunji0119/wanted-pre-onboarding-frontend

Contribute to Joeunji0119/wanted-pre-onboarding-frontend development by creating an account on GitHub.

github.com

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€