-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-refresh-token.ts
40 lines (33 loc) · 1.03 KB
/
use-refresh-token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { useMutation } from '@tanstack/react-query';
import { END_POINT } from '~/constants/api';
import { getToken } from '~/utils/api';
import { authServices } from '~/utils/auth';
import { authHttp } from '~/utils/http';
import type { Tokens } from './use-sign-in';
interface RefreshToken {
refreshToken: string;
}
const useRefreshTokens = () => {
const { setTokens, logout } = authServices();
const refreshMutation = useMutation({
mutationFn: () => {
const [accessToken, refreshToken] = getToken();
if (!accessToken || !refreshToken) {
throw new Error('토큰이 존재하지 않습니다');
}
const response = authHttp.post<RefreshToken>(END_POINT.REISSUE, {
json: { refreshToken },
});
return response.json<Tokens>();
},
onSuccess: (newTokens) => {
setTokens(newTokens);
},
onError: () => {
alert('오류가 발생하여 로그아웃합니다.');
logout();
},
});
return refreshMutation;
};
export { type RefreshToken, useRefreshTokens };