From cc9c96108dc52d266fb9e431fbd0da77eae96ce1 Mon Sep 17 00:00:00 2001 From: mackt <1033530438@qq.com> Date: Tue, 7 May 2024 23:35:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E6=9D=83=E9=99=90=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=20=E6=B8=85=E9=99=A4localstorage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/user.ts | 1 - src/utils/auth.ts | 32 +++++++++++++++++-------------- src/utils/request/client/utils.ts | 7 ++++++- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/store/user.ts b/src/store/user.ts index 5598eb3..29ac8f7 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -4,7 +4,6 @@ */ import { produce } from 'immer'; -// import { setCookie } from 'nookies'; import { create } from 'zustand'; import { devtools, persist, createJSONStorage } from 'zustand/middleware'; diff --git a/src/utils/auth.ts b/src/utils/auth.ts index f9bdffb..722b74d 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -11,20 +11,24 @@ export const checkAuthOffline: () => boolean = () => { }; export const getAuthorization = async () => { - const { token, expires } = await parseCookies(); - if (!token) return; - - if (token && !isExpToken(parseInt(expires ?? 0))) { - return token; - } else if (token) { - logout(); - // return tokenRefresh(); - // token 过期了,重新登录请求然后给请求头设置好token - // 获取token的方法等也可以根据authType的不同进行自定义设置 - // const res = await getToken("账号密码等"); - // setSession("local", tokenKey, res?.data); - // authorization = res?.data; - } + const { token } = await parseCookies(); + return token || ''; + // if (!token) return ''; + // const exp = parseInt(expires ?? 0); + // console.log(111, !isExpToken(exp)); + + // if (!isExpToken(exp)) { + // return token; + // } else { + // logout(); + // return ''; + // return tokenRefresh(); + // token 过期了,重新登录请求然后给请求头设置好token + // 获取token的方法等也可以根据authType的不同进行自定义设置 + // const res = await getToken("账号密码等"); + // setSession("local", tokenKey, res?.data); + // authorization = res?.data; + // } }; export function setAccessToken({ token, expires }: { token: string; expires: string }) { diff --git a/src/utils/request/client/utils.ts b/src/utils/request/client/utils.ts index aceb1ee..26a91d5 100644 --- a/src/utils/request/client/utils.ts +++ b/src/utils/request/client/utils.ts @@ -67,7 +67,12 @@ export const checkResponseState = async (response: FetchResponse) => { */ if ([400, 401, 10000].includes(code)) { // 判断是否有 token - if (checkAuthOffline()) logout(); + if (checkAuthOffline()) { + logout(); + window.location.reload(); + } else { + localStorage.clear(); + } // 打开登录框 openLogin();