cookie 가져오기
react-cookie 설치
npm install react-cookie
setting
import { Cookies } from 'react-cookie'
const cookies = new Cookies
export const setCookie = (name: string, value: string, option?: any) => {
return cookies.set(name, value, { ...option })
}
export const getCookie = (name: string) => {
return cookies.get(name)
}
cookie 저장
const jwtToken = await signIn(signInPayload)
if (jwtToken) {
setCookie('myToken', token, {
path: '/',
secure: true,
sameSiteL 'none'
})
}
cookie 사용
await axios({
method: 'get',
url: 'api.example.con/auth/currentUser',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ${getCookie('myToken')}'
}
})
참조 : https://blog.pumpkin-raccoon.com/80