-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 로그인 토큰 인증 post 요청 쿼리 훅 작성 #179
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |