fix(auth): 权限异常 清除localstorage

feature/artists
mackt 7 months ago
parent 7def728351
commit cc9c96108d

@ -4,7 +4,6 @@
*/ */
import { produce } from 'immer'; import { produce } from 'immer';
// import { setCookie } from 'nookies';
import { create } from 'zustand'; import { create } from 'zustand';
import { devtools, persist, createJSONStorage } from 'zustand/middleware'; import { devtools, persist, createJSONStorage } from 'zustand/middleware';

@ -11,20 +11,24 @@ export const checkAuthOffline: () => boolean = () => {
}; };
export const getAuthorization = async () => { export const getAuthorization = async () => {
const { token, expires } = await parseCookies(); const { token } = await parseCookies();
if (!token) return; return token || '';
// if (!token) return '';
if (token && !isExpToken(parseInt(expires ?? 0))) { // const exp = parseInt(expires ?? 0);
return token; // console.log(111, !isExpToken(exp));
} else if (token) {
logout(); // if (!isExpToken(exp)) {
// return token;
// } else {
// logout();
// return '';
// return tokenRefresh(); // return tokenRefresh();
// token 过期了重新登录请求然后给请求头设置好token // token 过期了重新登录请求然后给请求头设置好token
// 获取token的方法等也可以根据authType的不同进行自定义设置 // 获取token的方法等也可以根据authType的不同进行自定义设置
// const res = await getToken("账号密码等"); // const res = await getToken("账号密码等");
// setSession("local", tokenKey, res?.data); // setSession("local", tokenKey, res?.data);
// authorization = res?.data; // authorization = res?.data;
} // }
}; };
export function setAccessToken({ token, expires }: { token: string; expires: string }) { export function setAccessToken({ token, expires }: { token: string; expires: string }) {

@ -67,7 +67,12 @@ export const checkResponseState = async (response: FetchResponse<any>) => {
*/ */
if ([400, 401, 10000].includes(code)) { if ([400, 401, 10000].includes(code)) {
// 判断是否有 token // 判断是否有 token
if (checkAuthOffline()) logout(); if (checkAuthOffline()) {
logout();
window.location.reload();
} else {
localStorage.clear();
}
// 打开登录框 // 打开登录框
openLogin(); openLogin();

Loading…
Cancel
Save