forked from kookmin-sw/cap-template
-
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.
feat: Add Authorization Error Handler (kookmin-sw#83)
- Loading branch information
Showing
2 changed files
with
54 additions
and
50 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 |
---|---|---|
@@ -1,66 +1,71 @@ | ||
'use client'; | ||
|
||
import axios, { | ||
type AxiosError, | ||
type AxiosRequestConfig, | ||
type AxiosResponse, | ||
} from 'axios'; | ||
import { useRouter } from 'next/navigation'; | ||
import axios, { type AxiosError, type AxiosResponse } from 'axios'; | ||
|
||
import { getUserData, postTokenRefresh, useAuthActions } from '@/features/auth'; | ||
import { load } from '@/shared/storage'; | ||
import { postTokenRefresh } from '@/features/auth'; | ||
import { load, save } from '@/shared/storage'; | ||
import { type FailureDTO } from '@/shared/types'; | ||
|
||
let isRefreshing = false; | ||
let refreshSubscribers: Array<(token: string) => void> = []; | ||
|
||
const subscribeTokenRefresh = (callback: (token: string) => void) => { | ||
refreshSubscribers.push(callback); | ||
}; | ||
|
||
const onRefreshed = (token: string) => { | ||
refreshSubscribers.forEach(callback => { | ||
callback(token); | ||
}); | ||
refreshSubscribers = []; | ||
}; | ||
|
||
axios.interceptors.response.use( | ||
(response: AxiosResponse) => response, | ||
async (error: AxiosError) => { | ||
const router = useRouter(); | ||
const { login, logout, setAuthUserData } = useAuthActions(); | ||
async (error: AxiosError<FailureDTO>) => { | ||
if (error.response?.status !== 401 || error.response?.data.code !== 'C002') | ||
return await Promise.reject(error); | ||
|
||
const { config, response } = error; | ||
const originalRequest = config as AxiosRequestConfig; | ||
|
||
const refreshToken = load<string>({ | ||
type: 'local', | ||
key: 'refreshToken', | ||
}); | ||
const refreshToken = load<string>({ type: 'local', key: 'refreshToken' }); | ||
if (refreshToken == null) { | ||
window.location.href = '/'; | ||
return await Promise.reject(error); | ||
} | ||
|
||
if ( | ||
response != null && | ||
refreshToken != null && | ||
response.status === 401 && | ||
!isRefreshing | ||
) { | ||
isRefreshing = true; | ||
if (isRefreshing) { | ||
return await new Promise(resolve => { | ||
subscribeTokenRefresh((token: string) => { | ||
const { config } = error; | ||
if (config?.headers != null) { | ||
config.headers.Authorization = `Bearer ${token}`; | ||
resolve(axios(config)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
return await new Promise((resolve, reject) => { | ||
postTokenRefresh(refreshToken) | ||
.then(({ data }) => { | ||
if (originalRequest.headers != null) | ||
originalRequest.headers.Authorization = `Bearer ${data.accessToken}`; | ||
isRefreshing = true; | ||
try { | ||
const { | ||
data: { accessToken, refreshToken: newRefreshToken, expiresIn }, | ||
} = await postTokenRefresh(refreshToken); | ||
|
||
login(data); | ||
getUserData() | ||
.then(({ data: user }) => { | ||
setAuthUserData(user); | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
}); | ||
axios.defaults.headers.common.Authorization = `Bearer ${accessToken}`; | ||
save({ type: 'local', key: 'refreshToken', value: newRefreshToken }); | ||
save({ type: 'local', key: 'expiresIn', value: `${expiresIn}` }); | ||
|
||
resolve(axios(originalRequest)); | ||
}) | ||
.catch(err => { | ||
router.push('/'); | ||
logout(); | ||
reject(err); | ||
}) | ||
.finally(() => { | ||
isRefreshing = false; | ||
}); | ||
}); | ||
const { config } = error; | ||
onRefreshed(accessToken); | ||
if (config != null) { | ||
config.headers.Authorization = `Bearer ${accessToken}`; | ||
return await axios(config); | ||
} | ||
} catch (refreshError) { | ||
return await Promise.reject(error); | ||
} finally { | ||
isRefreshing = false; | ||
} | ||
|
||
return await Promise.reject(error); | ||
}, | ||
); |
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