fix: add optional chaining for safe access to response headers in auth middleware

This commit is contained in:
sheldon guo 2025-02-13 09:55:44 +08:00
parent 1834cd6f05
commit 5393e3ddd8

View File

@ -42,20 +42,20 @@ const debouncedRedirect = debounce(
export function authCheckMiddleware({ app }: { app: Application }) {
const axios = app.apiClient.axios;
const resHandler = (res: AxiosResponse) => {
const newToken = res.headers['x-new-token'];
const newToken = res?.headers?.['x-new-token'];
if (newToken) {
app.apiClient.auth.setToken(newToken);
}
return res;
};
const errHandler = (error) => {
const newToken = error.response.headers['x-new-token'];
const newToken = error?.response?.headers?.['x-new-token'];
if (newToken) {
app.apiClient.auth.setToken(newToken);
}
if (error.status === 401 && !error.config?.skipAuth) {
const requestToken = error?.config?.headers.Authorization?.replace(/^Bearer\s+/gi, '');
const requestToken = error?.config?.headers?.Authorization?.replace(/^Bearer\s+/gi, '');
const currentToken = app.apiClient.auth.getToken();
if (currentToken && currentToken !== requestToken) {
error.config.skipNotify = true;