[Nextjs]using token in getServerSideProps

이예린·2022년 11월 14일
0

로그인 성공시 token cookie에 저장

document.cookie = `at=${accessToken}`;
document.cookie = `rt=${refreshToken}`;

list/index.tsx

//...생략

export async function getServerSideProps(context: any) {
  axios.defaults.baseURL = 'http://localhost:3000';
  axios.defaults.withCredentials = true;
  axios.defaults.headers.common[
    'Authorization'
  ] = `Bearer ${context?.req?.cookies.at}`;

  const result = await axios.get('/api/study');
  const { data } = result;

  return {
    props: { list: data?.studies },
  };
}

0개의 댓글