Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] 리프레시 토큰 적용 #234

Closed
nan-noo opened this issue Aug 9, 2022 · 1 comment · Fixed by #244
Closed

[FE] 리프레시 토큰 적용 #234

nan-noo opened this issue Aug 9, 2022 · 1 comment · Fixed by #244
Assignees
Labels
🚀 feature New feature or request 😁 frontend New frontend feature
Projects
Milestone

Comments

@nan-noo
Copy link
Collaborator

nan-noo commented Aug 9, 2022

요약

  • accessToken이 만료되었다는 응답이 왔을 때 refreshToken과 함께 accessToken을 발급 요청한다
  • 새로운 accessToken을 발급받으면 accessToken을 클라이언트에 저장하고, 기존에 요청했던 리소스를 다시 fetch
  • refreshToken도 만료되었다는 응답이 오면 저장되어 있는 accessToken을 제거하고 로그아웃

세부 설명

axios 또는 react-query를 사용하여 에러 핸들링을 할 예정이다.

axiosinstance에 response interceptors로 response 에러가 발생했을 때를 처리할 수 있는데, 에러 코드가 accessToken 만료를 의미한다면 refreshToken 재발급 api로 요청을 보내 새로운 accessToken을 세션 스토리지에 저장한다.(이 과정이 실패할 때 에러 처리도 필요)
-> request interceptors의 config에 항상 최신의 accessToken을 header 'Authorization' 필드에 저장하도록 되어 있음!

react-query 훅의 onError 옵션을 활용한다
코드 참고

useQuery(key, fn, {
    onError: (error) => {
      if (새로운 accessToken 재발급 받음) {
          queryClient.invalidateQueries("todos"); // 또는 refetch
          return;
      }
      ...
    }
})

처음 로그인할 때 refreshToken을 쿠키에 저장하도록 구현


또 다른 방법!
그냥 react query onError만 사용한다.

useQuery(key, fn, {
    onError: (error) => {
      if (accessToken이 만료되었다) { 
        // 아래 코드도 useMutation 훅으로 빼도 될 듯
        postRefreshToken().then((tokenRefreshSucccessful) =>
          queryClient.invalidateQueries("todos") // refetch
        ).catch(()=>{ 리프레시 토큰도 문제가 있는 것이므로 로그아웃 필요 })
      }
    }
})
@nan-noo nan-noo added 🚀 feature New feature or request 😁 frontend New frontend feature labels Aug 9, 2022
@nan-noo nan-noo added this to the Milestone 4 milestone Aug 9, 2022
@nan-noo nan-noo self-assigned this Aug 9, 2022
@nan-noo nan-noo added this to To do in Sprint 4 via automation Aug 9, 2022
@nan-noo
Copy link
Collaborator Author

nan-noo commented Aug 9, 2022

우선 두번째 방법으로 진행!

@tco0427 tco0427 moved this from To do to In progress in Sprint 4 Aug 10, 2022
@nan-noo nan-noo linked a pull request Aug 11, 2022 that will close this issue
@nan-noo nan-noo closed this as completed Aug 13, 2022
@tco0427 tco0427 moved this from In progress to Done in Sprint 4 Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 feature New feature or request 😁 frontend New frontend feature
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

1 participant