Skip to content

Commit

Permalink
feat: 로그인 토큰 인증 post 요청 쿼리 훅 작성 #179
Browse files Browse the repository at this point in the history
  • Loading branch information
seoye0ng committed Feb 20, 2024
1 parent a44cb1c commit 64ae761
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/remote/queries/auth/useRefreshToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable no-console */
import { useCookies } from 'react-cookie';

import { useMutation } from '@tanstack/react-query';

import useLoggedOut from '@/hooks/useLoggedOut';
import { refreshToken } from '@remote/api/requests/auth/auth.post.api';
import { RefreshTokenType } from '@remote/api/types/auth';

function useRefreshToken() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [cookies, setCookie, removeCookie] = useCookies(['token']);
const handleLogout = useLoggedOut('/login');

const onSuccess = (data: RefreshTokenType) => {
const { jwtToken } = data.value;
const cookieOptions = { path: '/', maxAge: 60 * 15 };

setCookie('token', jwtToken, cookieOptions);
};

const onError = () => {
handleLogout('/login');
};

return useMutation({
mutationFn: refreshToken,
onSuccess,
onError,
});
}

export default useRefreshToken;

0 comments on commit 64ae761

Please sign in to comment.