-
Notifications
You must be signed in to change notification settings - Fork 0
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 #98 from capstone-maru/feat/error-handler
feat: Add Error Handling
- Loading branch information
Showing
9 changed files
with
309 additions
and
169 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,71 @@ | ||
'use client'; | ||
|
||
import axios, { type AxiosError, type AxiosResponse } from 'axios'; | ||
|
||
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<FailureDTO>) => { | ||
if (error.response?.status !== 401 || error.response?.data.code !== 'C002') | ||
return await Promise.reject(error); | ||
|
||
const refreshToken = load<string>({ type: 'local', key: 'refreshToken' }); | ||
if (refreshToken == null) { | ||
window.location.href = '/'; | ||
return await Promise.reject(error); | ||
} | ||
|
||
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)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
isRefreshing = true; | ||
try { | ||
const { | ||
data: { accessToken, refreshToken: newRefreshToken, expiresIn }, | ||
} = await postTokenRefresh(refreshToken); | ||
|
||
axios.defaults.headers.common.Authorization = `Bearer ${accessToken}`; | ||
save({ type: 'local', key: 'refreshToken', value: newRefreshToken }); | ||
save({ type: 'local', key: 'expiresIn', value: `${expiresIn}` }); | ||
|
||
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
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
Oops, something went wrong.