-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: axisos interceptor 토큰 재발급 기능 추가 (#180)
* refactor: 토큰 쿠키에서 관리하도록 수정 * chore: react-cookie 설치 * refactor: 기존 localToken 사용하던 부분 쿠키로 수정 * chore: 쿠키 key값 상수화 * feat: token 데이터에 currentUser도 추가 * chore: moment 라이브러리 추가 * feat: 토큰 유효기간 10분 기준으로 재발급 유무를 결정하는 checkToken 함수 생성 * fix: 쿠키가 전체 path에서 사용가능하도록 수정 * chore: 불필요한 코드 제거
- Loading branch information
Showing
6 changed files
with
65 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
import { AxiosRequestConfig } from 'axios' | ||
import { getTokenData, setToken } from '@utils/cookie' | ||
import moment from 'moment' | ||
import axios from 'axios' | ||
|
||
export const checkToken = async (config: AxiosRequestConfig) => { | ||
const { currentUser, tastyToken, tastyToken_expire, tastyRefreshToken } = | ||
getTokenData() | ||
|
||
if (!config?.headers) { | ||
throw new Error(`Axios config headers must be provided`) | ||
} | ||
|
||
if ( | ||
moment(tastyToken_expire).diff(moment(), 'minutes') < 10 && | ||
tastyRefreshToken | ||
) { | ||
const { data } = await axios.post( | ||
`${process.env.NEXT_PUBLIC_API_URL}/re-issue`, | ||
{ | ||
email: currentUser.email, | ||
accessToken: tastyToken, | ||
refreshToken: tastyRefreshToken | ||
} | ||
) | ||
setToken(data) | ||
config.headers.Authorization = data.token | ||
} else { | ||
config.headers.Authorization = tastyToken | ||
} | ||
return config | ||
} |
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