accessToken & Hooks๐Ÿช

alert("april");ยท2023๋…„ 10์›” 18์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
16/17
post-custom-banner

์‚ฌ์‹ค์€ accessToken์„ ์“ฐ๋ ค๋ฉด
๋จผ์ € ๊ฒ€์‚ฌ๋ฅผ ํ•ด์ค˜์•ผํ•จ
๋ญ๋ฅผ? accessToken์ด null ์ธ์ง€ ์•„๋‹Œ์ง€

์ƒํ™ฉ 1) ๋กœ๊ทธ์ธ์„ ์•ˆํ•ด์„œ null

-> localStorage์—๋„ ์—†์Œ

์ƒํ™ฉ 2) ๋กœ๊ทธ์ธ์„ ํ–ˆ๋Š”๋ฐ ์ƒˆ๋กœ๊ณ ์นจ ํ–ˆ์Œ

-> localStorage์—๋Š” ์žˆ๋Š”๋ฐ accessToken์—๋Š” ์—†์Œ

=> useEffect( ) ๋ฅผ ์จ์•ผํ•œ๋‹ค

๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” hooks(hook ํ•จ์ˆ˜)๋ฅผ ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค!!


<๊ฐ•์‚ฌ๋‹˜ ์„ค๋ช…>
Dashboardheader
CareerViewTable
...
๋กœ๊ทธ์ธ์„ ํ•ด์•ผ์ง€๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์ด ์กด์žฌ
--> accessToken์„ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ๋“ค
ํ•ญ์ƒ accessToken์ด ์ž˜ ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌ
--> ์ฆ‰ useEffect ์—๋‹ค๊ฐ€ accessToken์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธ ํ•„์š”ํ•˜๋‹ค
๋งค๋ฒˆ useEffect๋ฅผ ๊ธธ๊ฒŒ ์“ฐ๊ธฐ ๊ท€์ฐฎ์œผ๋‹ˆ๊นŒ custom hook์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค


Hook ํ•จ์ˆ˜๋Š”
์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ X
์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ X
Q. ๊ทธ๋Ÿผ ์–ด๋””์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ ๊นŒ?
-useEffect ๋‚˜ ๋‹ค๋ฅธ useํ›…ํ•จ์ˆ˜๊ฐ€ ๊ทธ๋ ค์งˆ๋•Œ ์‚ฌ์šฉ

hooks.js

import { useContext, useEffect } from "react";
import { UserContext } from "../App";
import { useNavigate } from "react-router-dom";

// accessToken์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ํ›…ํ•จ์ˆ˜
export const useAuth = ()=>{
    const {accessToken, setAccessToken} = useContext(UserContext);
    const navigate = useNavigate();
    useEffect(()=>{
        //  1. ๋กœ๊ทธ์ธ์ด ์•ˆ๋œ ์ƒํƒœ localStorage์˜ accessToken null,
        //  ์ „์—ญ ์ƒํƒœ๋ณ€์ˆ˜ accessToken null
        if(localStorage.getItem('accessToken') === null){
            alert('๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ํŽ˜์ด์ง€ ์ž…๋‹ˆ๋‹ค');
            navigate('/login', {replace : true});
            return;
        }

        //  2. ๋กœ๊ทธ์ธ์€ ๋˜์—ˆ์œผ๋‚˜ ์ƒˆ๋กœ๊ณ ์นจํ•œ ์ƒํƒœ accessToken ์žˆ์Œ,
        //  ์ „์—ญ ์ƒํƒœ๋ณ€์ˆ˜ accessToken null
        if(accessToken === null){
            setAccessToken(localStorage.getItem('accessToken'));
            return;
        }

        //  3. ๋กœ๊ทธ์ธ ๋˜์—ˆ๊ณ , ์ƒˆ๋กœ๊ณ ์นจ๋„ ์•ˆํ•จ
        //  localStorage accessToken ์žˆ์Œ,
        //  ์ „์—ญ ์ƒํƒœ๋ณ€์ˆ˜ accessToken ์žˆ์Œ
        // => ๋ญ”๊ฐ€๋ฅผ ์‹คํ–‰์‹œ์ผœ์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค

    }, [accessToken, setAccessToken, navigate]);
}

// ์šฐ๋ฆฌ๊ฐ€ accessToken์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๊ณณ์—๋‹ค๊ฐ€ ์ด๊ฑธ ์จ์ค˜์•ผ ํ•œ๋‹ค(career.js์— ์ ๊ธฐ)


// ํŽ˜์ด์ง€ ์ด๋™์„ ์œ„ํ•œ hook ํ•จ์ˆ˜
export const useMove = (dir)=>{
    const navigate = useNavigate();
    return ()=>{navigate(dir)}
}

// ํŽ˜์ด์ง€ ์ด๋™์ธ๋ฐ ๋’ค๋กœ๊ฐ€๊ธฐ ๋ชปํ•˜๊ฒŒ ์ด๋™ํ•˜๋Š” hook ํ•จ์ˆ˜
export const useReplace = (dir)=>{
    const navigate = useNavigate();
    navigate(dir, {replace : true});
}

์—‡...CareerPage ์ž์ฒด๊ฐ€ ๋กœ๊ทธ์ธ ํ•ด์•ผ์ง€๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€๋„ค? ๊ทธ๋Ÿผ ์ €๊ธฐ๋‹ค ๋ฐ”๋กœ ์จ์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค

career.js

import CareerViewTable from "../../components/career/careerViewTable";
import DashboardLayout from "../../components/common/layout";
import { useAuth } from "../../hooks/hooks";
import { Title } from "../../styles/dashboard/career.styles";


const CareerPage = ()=>{
  useAuth(); // ์ด ํŽ˜์ด์ง€ ์ž์ฒด๊ฐ€ ๋กœ๊ทธ์ธ ํ•ด์•ผ์ง€๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€๋„ค..?

  return(
    <DashboardLayout>
      <Title>๋‚˜์˜ ๊ฒฝ๋ ฅ์„ ๊ด€๋ฆฌํ•˜์„ธ์š”</Title>
      <p>ํšŒ์‚ฌ, ์ง์œ„, ์ผ์ž๋ฅผ ์ž…๋ ฅํ•œ ํ›„ ๊ฒฝ๋ ฅ์„ ์ถ”๊ฐ€ํ•ด ๋ณด์„ธ์š”!</p>
      <CareerViewTable/>
    </DashboardLayout>
  );
}

export default CareerPage;

๊ทธ๋Ÿผ ์ด์ œ accessToken ๊ฐ์ฒด๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค

{ headers : {Authorization : `Bearer ${localStorage.getItem('accessToken')}`}}
=> {headers : {Authorization:`Bearer ${accessToken}`}}

headers.js

import { useContext, useEffect, useState } from "react";
import { UserText } from "../../styles/common/aside.styles";
import { Header } from "../../styles/common/header.styles";
import axios from "axios";
import { UserContext } from "../../App";
import { useNavigate } from "react-router-dom";
import MenuIcon from '@mui/icons-material/Menu';
import MenuOpenIcon from '@mui/icons-material/MenuOpen';
import { Popover } from "@mui/material";

const DashboardHeader = ()=>{

  const navigate = useNavigate();
  // ์ „์—ญstate๋ณ€์ˆ˜์— ์žˆ๋Š” ํ† ํฐ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
  const {accessToken, setAccessToken} = useContext(UserContext);
  const [loggedInUser, setLoggedInUser] = useState( { 
    email:'๋กœ๊ทธ์ธํ›„ ์ด์šฉํ•ด์ฃผ์„ธ์š”',
    created_date:'',
    updated_date:''
  } );
  // const [loggedInEmail , setLoggedInEmail] = useState('๋กœ๊ทธ์ธํ›„ ์ด์šฉํ•ด์ฃผ์„ธ์š”');

  // popOver ์ฐฝ์˜ ๊ธฐ์ค€ element ์š”์†Œ
  const [anchorEl, setAnchorEl] = useState(null);
  let open = Boolean(anchorEl);

  // ํ—ค๋”๊ฐ€ ๊ทธ๋ ค์ง€๋ฉด db๊ฐ€์„œ ๋กœ๊ทธ์ธ ํ•œ ์‚ฌ๋žŒ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
  useEffect(()=>{

    let tmp = async ()=>{
      if(accessToken === null) return;
      try{
        let res = await axios.get('/api/loggedInEmail', 
          {headers : {Authorization : `Bearer ${accessToken}`}}
        );
        console.log('res', res);
	.
	.
	.
        

careerViewTable.js

import { useContext, useEffect, useState } from "react";
import { AddBox } from "../../styles/dashboard/career.styles";
import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
import axios from "axios";
import { UserContext } from "../../App";

const CareerViewTable = ()=>{
  const [careerList, setCareerList] = useState([]);
  // ํ† ํฐ์ด ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ „์—ญ ์ƒํƒœ๋ณ€์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ
  const {accessToken , setAccessToken} = useContext(UserContext);

  useEffect( ()=>{
    // CareerViewTable์ด ์ตœ์ดˆ ๋ Œ๋”๋ง ๋ ๋•Œ
    // expressํ•œํ…Œ career๋ชฉ๋ก์ข€ ๊ฐ€์ ธ๋‹ค์ค˜ ๋ผ๊ณ  ์š”์ฒญ
    const fetchCareerList = async ()=>{
      if(accessToken === null) return;
      try{
        let res = await axios.get('/api/career', 
          {headers : {Authorization:`Bearer ${accessToken}`}}
        );
        setCareerList(res.data);
      }catch(err){
        console.log(err);
      }
    }

    fetchCareerList();

  }, [accessToken]); // accessToken์ด ๋ฐ”๋€Œ๋ฉด ๋‹ค์‹œ ํ•œ๋ฒˆ ์‹คํ–‰์‹œ์ผœ์ค˜

  .
  .
  .

React๊ฐ€ ์ ์  ๋ณต์žกํ•ด์ง„๋‹ค...์›๋ž˜ ํ”„๋ก ํŠธ๋ฅผ ๊ฐ€์žฅ ์ข‹์•„ํ–ˆ๋Š”๋ฐ..์œฝ
๋จธ๋ฆฌ์— ์ฅ๊ฐ€ ๋‚ ๊ฒƒ ๊ฐ™๋‹ค...๐Ÿญ๐Ÿญ๐Ÿญ

profile
Slowly but surely
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€