2023. 01. 17

홍왕열·2023년 1월 17일
0

TIL

목록 보기
48/56
post-thumbnail

cookie 가져오기

npm install react-cookie // yarn add 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)
}
const jwtToken = await signIn(signInPayload)
if (jwtToken) {
	setCookie('myToken', token, { //이게 핵심, setCookie
      path: '/',
      secure: true,
      sameSiteL 'none'
    })
}
await axios({
  method: 'get',
  url: 'api.example.con/auth/currentUser',
  headers: {
  	'Content-Type': 'application/json',
    Authorization: 'Bearer ${getCookie('myToken')}' //getCookie(name)
  }
})

참조 : https://blog.pumpkin-raccoon.com/80

profile
코딩 일기장

0개의 댓글