Skip to content

Commit

Permalink
New : 내 정보 체크실패시 로그아웃되도록 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
jobkaeHenry committed Dec 3, 2023
1 parent dbb67d0 commit f171a26
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions client/src/queries/auth/useMyInfoQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@ import useAxiosPrivate from "@/hooks/useAxiosPrivate";
import { MyInfoInterface } from "@/types/auth/myInfo";
import getTokenFromLocalStorage from "@/utils/getTokenFromLocalStorage";
import { useQuery } from "@tanstack/react-query";
import { isAxiosError } from "axios";
import { useRouter } from "next/navigation";
import useLogoutMutation from "./useLogoutMutation";

export const useMyInfoQuery = () =>
useQuery({
export const useMyInfoQuery = () => {
const router = useRouter();
const { mutate: logout } = useLogoutMutation();

return useQuery({
queryKey: MyInfoQueryKeys.all,
queryFn: getMyInfoByLocalStorage,
queryFn: async () => {
try {
return await getMyInfoByLocalStorage();
} catch (err) {
// FIXME 토큰만료체크시 로그아웃 로직 분리 필요
if (isAxiosError(err) && err.response) {
if (err.response.status === 401 && getTokenFromLocalStorage()) {
logout();
}
}
throw Error();
}
},
});
};

export const getMyInfoByLocalStorage = async () => {
const accessToken = getTokenFromLocalStorage();

if (!accessToken) {
return null;
}
Expand Down

0 comments on commit f171a26

Please sign in to comment.