mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-05 23:57:19 +08:00
feat(api): handle 401 status and improve authentication flow
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import type { BaseResult } from './types';
|
import type { BaseResult } from './types';
|
||||||
|
import { clearAuthData } from './index';
|
||||||
export const BASE_URL = import.meta.env.PROD ? '/api' : 'http://localhost:5153/api';
|
export const BASE_URL = import.meta.env.PROD ? '/api' : 'http://localhost:5153/api';
|
||||||
|
|
||||||
export async function fetchApi<T = any>(
|
export async function fetchApi<T = any>(
|
||||||
@@ -18,6 +19,18 @@ export async function fetchApi<T = any>(
|
|||||||
...options,
|
...options,
|
||||||
headers,
|
headers,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (response.status === 401) {
|
||||||
|
clearAuthData();
|
||||||
|
const message = encodeURIComponent('授权过期重新登录');
|
||||||
|
window.location.href = `/login?message=${message}`;
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: '授权过期重新登录',
|
||||||
|
code: 401,
|
||||||
|
} as BaseResult<T>;
|
||||||
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return data as BaseResult<T>;
|
return data as BaseResult<T>;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export {
|
|||||||
register,
|
register,
|
||||||
login,
|
login,
|
||||||
getCurrentUser,
|
getCurrentUser,
|
||||||
updateUserInfo, // 添加导出更新用户信息函数
|
updateUserInfo,
|
||||||
saveAuthData,
|
saveAuthData,
|
||||||
clearAuthData,
|
clearAuthData,
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
|
|||||||
Reference in New Issue
Block a user