์ฌ์ค์ accessToken์ ์ฐ๋ ค๋ฉด
๋จผ์ ๊ฒ์ฌ๋ฅผ ํด์ค์ผํจ
๋ญ๋ฅผ? accessToken์ด null ์ธ์ง ์๋์ง
-> localStorage์๋ ์์
-> localStorage์๋ ์๋๋ฐ accessToken์๋ ์์
=> useEffect( ) ๋ฅผ ์จ์ผํ๋ค
๊ทธ๋์ ์ฐ๋ฆฌ๋ hooks(hook ํจ์)๋ฅผ ์ง์ ๋ง๋ค ์ ์๋ค!!
<๊ฐ์ฌ๋ ์ค๋ช
>
Dashboardheader
CareerViewTable
...
๋ก๊ทธ์ธ์ ํด์ผ์ง๋ง ๋ณผ ์ ์๋ ์ปดํฌ๋ํธ๋ค์ด ์กด์ฌ
--> accessToken์ ์ฌ์ฉํ๋ ์ปดํฌ๋ํธ ๋ค
ํญ์ accessToken์ด ์ ์กด์ฌํ๋์ง๋ฅผ ๊ฒ์ฌ
--> ์ฆ useEffect ์๋ค๊ฐ accessToken์ด ์๋์ง ์๋์ง ํ์ธ ํ์ํ๋ค
๋งค๋ฒ useEffect๋ฅผ ๊ธธ๊ฒ ์ฐ๊ธฐ ๊ท์ฐฎ์ผ๋๊น custom hook์ ๋ง๋ค ์ ์๋ค
Hook ํจ์๋
์ผ๋ฐ ํจ์์์ ์ฌ์ฉ X
์ฝ๋ฐฑ ํจ์์์ ์ฌ์ฉ X
Q. ๊ทธ๋ผ ์ด๋์ ์ฌ์ฉํ๋ฉด ๋ ๊น?
-useEffect ๋ ๋ค๋ฅธ useํ
ํจ์๊ฐ ๊ทธ๋ ค์ง๋ ์ฌ์ฉ
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 ์์ฒด๊ฐ ๋ก๊ทธ์ธ ํด์ผ์ง๋ง ๋ณผ ์ ์๋ ํ์ด์ง๋ค? ๊ทธ๋ผ ์ ๊ธฐ๋ค ๋ฐ๋ก ์จ์ฃผ๋ฉด ๋๊ฒ ๋ค
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}`}}
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);
.
.
.
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๊ฐ ์ ์ ๋ณต์กํด์ง๋ค...์๋ ํ๋ก ํธ๋ฅผ ๊ฐ์ฅ ์ข์ํ๋๋ฐ..์ฝ
๋จธ๋ฆฌ์ ์ฅ๊ฐ ๋ ๊ฒ ๊ฐ๋ค...๐ญ๐ญ๐ญ