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

[feat] 토큰 검증 실패 시 토큰 제거 #124

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/components/common/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { AxiosError } from 'axios';
import Link from 'next/link';
import { FC, useState } from 'react';
import { useRecoilValue, useResetRecoilState } from 'recoil';

import { ErrorResponse } from '@src/apis';
import Icon from '@src/components/common/Icon';
import UserInfo from '@src/components/common/UserInfo';
import useMember from '@src/queires/useMember';
Expand All @@ -12,8 +14,17 @@ import UserMenu from './UserMenu';

const Header: FC = () => {
const userSession = useRecoilValue($userSession);
const { data: member } = useMember(userSession?.accessToken);
const resetUser = useResetRecoilState($userSession);
const { data: member } = useMember(userSession?.accessToken, {
onError: (err) => {
const error = err as AxiosError<ErrorResponse>;
const code = error.response?.data.code;
if (Number(code) !== 2003) return;

resetUser();
},
});

Comment on lines +18 to +27
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#123 과 연관되어 있지만 해당 부분 구현하셔서 제가 생각했던 방법을 이야기하자면!

axios Response interceptor 에서 401 에러가 나면 refresh 토큰 발급하고 refresh 마저 에러가 나면 로그인이 안되는 방식으로 가면 어떨지 생각이 들었습니다!

어떻게 생각하시나요??

const [viewUserMenu, setViewUserMenu] = useState(false);

const handleLogout = () => {
Expand Down
5 changes: 3 additions & 2 deletions src/queires/useMember.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useQuery } from '@tanstack/react-query';
import { UseQueryOptions, useQuery } from '@tanstack/react-query';

import { GetMemberResponse, getMember } from '@src/apis';
import { queryKeys } from '@src/queires/constant';

const useMember = (token = '') => {
const useMember = (token = '', options?: UseQueryOptions<GetMemberResponse>) => {
const result = useQuery<GetMemberResponse>([queryKeys.profile, token], getMember, {
...options,
enabled: !!token,
});

Expand Down