-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from dnd-side-project/feat-166
[# 166] 리프레쉬 토큰을 이용한 액세스 토큰 재갱신 기능 구현
- Loading branch information
Showing
12 changed files
with
129 additions
and
19 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
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,11 @@ | ||
import { baseAxios } from '@/libs/baseAxios.ts'; | ||
|
||
export interface RefreshAccessTokenResponse { | ||
accessToken: string; | ||
expiresIn: number; | ||
refreshToken: string; | ||
refreshTokenExpiresIn: number; | ||
} | ||
|
||
export const refreshAccessToken = async (refreshToken: string) => | ||
baseAxios.post<RefreshAccessTokenResponse>('/token', { refreshToken }); |
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
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,54 @@ | ||
import { useCookies } from 'react-cookie'; | ||
import { useCallback } from 'react'; | ||
|
||
interface SetAccessTokenParams { | ||
token: string; | ||
expiresIn: number; | ||
} | ||
|
||
interface SetRefreshTokenParams { | ||
token: string; | ||
expiresIn: number; | ||
} | ||
|
||
export const useToken = () => { | ||
const [cookies, setCookie] = useCookies(['access-token', 'refresh-token']); | ||
|
||
const setAccessToken = useCallback( | ||
({ token, expiresIn }: SetAccessTokenParams) => { | ||
setCookie('access-token', token, { | ||
expires: new Date(Date.now() + expiresIn), | ||
}); | ||
}, | ||
[setCookie], | ||
); | ||
|
||
const setRefreshToken = useCallback( | ||
({ token, expiresIn }: SetRefreshTokenParams) => { | ||
setCookie('refresh-token', token, { | ||
expires: new Date(Date.now() + expiresIn), | ||
}); | ||
}, | ||
[setCookie], | ||
); | ||
|
||
const getRefreshToken = useCallback(() => { | ||
return cookies['refresh-token']; | ||
}, [cookies]); | ||
|
||
const getAccessToken = useCallback(() => { | ||
return cookies['access-token']; | ||
}, [cookies]); | ||
|
||
const isValidToken = useCallback((token?: any): token is string => { | ||
return typeof token === 'string'; | ||
}, []); | ||
|
||
return { | ||
setAccessToken, | ||
setRefreshToken, | ||
getRefreshToken, | ||
getAccessToken, | ||
isValidToken, | ||
}; | ||
}; |
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
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,6 @@ | ||
export const refreshAccessTokenResponseData = { | ||
accessToken: 'accessToken', | ||
expiresIn: 21599, | ||
refreshToken: 'refreshToken', | ||
refreshTokenExpiresIn: 2159900, | ||
}; |
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
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
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
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
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
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,9 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { keyStore } from '@/queries/keyStore.ts'; | ||
import { refreshAccessToken } from '@/apis/auth/refreshAccessToken.ts'; | ||
|
||
export const useRefreshAccessToken = () => | ||
useMutation({ | ||
mutationKey: keyStore.auth.refreshAccessToken.queryKey, | ||
mutationFn: refreshAccessToken, | ||
}); |