-
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.
* feat: update authentication and error handling in ApiClient and queryClient * test: update test config * feat: add requestManager for aborting requests and update onSuccess function * refactor: remove unused error types and BaseErrorBoundary component * fix: handle request cancellation and remove isSilentError function * fix: update request interceptor to use requestManager for aborting requests and handle logout and token refresh
- Loading branch information
1 parent
a83fbcc
commit 164b4b2
Showing
5 changed files
with
67 additions
and
25 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,38 +1,54 @@ | ||
import { useAuthStore } from "~/modules/Auth/authStore"; | ||
import { API } from "./generated-api"; | ||
|
||
export const requestManager = { | ||
controller: new AbortController(), | ||
|
||
abortAll() { | ||
this.controller.abort(); | ||
}, | ||
}; | ||
|
||
export const ApiClient = new API({ | ||
baseURL: import.meta.env.VITE_API_URL, | ||
secure: true, | ||
withCredentials: true, | ||
}); | ||
|
||
ApiClient.instance.interceptors.request.use((config) => { | ||
const isAuthEndpoint = | ||
config.url?.includes("/login") || | ||
config.url?.includes("/refresh") || | ||
config.url?.includes("/register"); | ||
|
||
if (!isAuthEndpoint && !useAuthStore.getState().isLoggedIn) { | ||
config.signal = requestManager.controller.signal; | ||
} | ||
|
||
return config; | ||
}); | ||
|
||
ApiClient.instance.interceptors.response.use( | ||
(response) => response, | ||
async (error) => { | ||
const originalRequest = error.config; | ||
|
||
const isLoginRequest = originalRequest.url.includes("/login"); | ||
const isRefreshRequest = originalRequest.url.includes("/refresh"); | ||
|
||
if (error.response?.status === 401 && !originalRequest._retry) { | ||
originalRequest._retry = true; | ||
|
||
if ( | ||
!isLoginRequest && | ||
!isRefreshRequest && | ||
useAuthStore.getState().isLoggedIn | ||
) { | ||
try { | ||
await ApiClient.api.authControllerRefreshTokens(); | ||
return ApiClient.instance(originalRequest); | ||
} catch (refreshError) { | ||
useAuthStore.getState().setLoggedIn(false); | ||
return Promise.reject(refreshError); | ||
} | ||
} | ||
if (error.config?.url?.includes("/logout")) { | ||
return Promise.reject(error); | ||
} | ||
|
||
if ( | ||
error.response?.status === 401 && | ||
!error.config._retry && | ||
useAuthStore.getState().isLoggedIn | ||
) { | ||
error.config._retry = true; | ||
try { | ||
await ApiClient.api.authControllerRefreshTokens(); | ||
return ApiClient.instance(error.config); | ||
} catch { | ||
requestManager.abortAll(); | ||
return Promise.reject(error); | ||
} | ||
} | ||
return 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