refactor(auth): standardize authorization token handling

- Replace 'token' with 'Authorization' in localStorage keys for consistency
- Update auth guard, request interceptor and login hooks to use new key
- Add updateAuthorization method to auth hook for better state management
This commit is contained in:
2025-10-11 14:20:56 +08:00
parent 650c41dc41
commit 6f0332c1ff
3 changed files with 69 additions and 57 deletions

View File

@@ -47,12 +47,12 @@ export type ResultCode =
// 获取授权token
const getAuthorization = (): string => {
return localStorage.getItem('token') || '';
return localStorage.getItem(Authorization) || '';
};
// 重定向到登录页
const redirectToLogin = (): void => {
localStorage.removeItem('token');
localStorage.removeItem(Authorization);
window.location.href = '/login';
};
@@ -94,9 +94,9 @@ request.interceptors.request.use(
}
// 添加授权头
const token = getAuthorization();
if (token && !config.headers?.skipToken) {
config.headers[Authorization] = token;
const authorization = getAuthorization();
if (authorization && !config.headers?.skipToken) {
config.headers.Authorization = authorization;
}
return config;