mirror of
https://github.com/KwiTsukasa/kt-template-online-web.git
synced 2026-05-27 16:35:47 +08:00
fix(web): 防止登录回跳死循环
This commit is contained in:
parent
64b911a5a4
commit
ed37ca5d12
@ -16,6 +16,8 @@ type PersistedAuth = {
|
||||
const ACCESS_TOKEN_KEY = "kt-admin-access-token";
|
||||
const ACCESS_CODES_KEY = "kt-admin-access-codes";
|
||||
const USER_INFO_KEY = "kt-admin-user-info";
|
||||
const LOGIN_REDIRECT_MARK_KEY = "kt-admin-login-redirect-at";
|
||||
const LOGIN_REDIRECT_COOLDOWN = 10 * 1000;
|
||||
|
||||
let refreshPromise: Promise<string | null> | null = null;
|
||||
let redirectingToAdminLogin = false;
|
||||
@ -36,6 +38,16 @@ export const clearPersistedAuth = () => {
|
||||
window.localStorage.removeItem(USER_INFO_KEY);
|
||||
};
|
||||
|
||||
export const clearAdminLoginRedirectMark = () => {
|
||||
window.sessionStorage.removeItem(LOGIN_REDIRECT_MARK_KEY);
|
||||
};
|
||||
|
||||
export const shouldSkipRepeatedAdminLoginRedirect = () => {
|
||||
const redirectAt = Number(window.sessionStorage.getItem(LOGIN_REDIRECT_MARK_KEY));
|
||||
|
||||
return Number.isFinite(redirectAt) && Date.now() - redirectAt < LOGIN_REDIRECT_COOLDOWN;
|
||||
};
|
||||
|
||||
export const persistAuthData = ({
|
||||
accessCodes,
|
||||
accessToken,
|
||||
@ -70,6 +82,7 @@ export const redirectToAdminLogin = () => {
|
||||
if (redirectingToAdminLogin) return;
|
||||
|
||||
redirectingToAdminLogin = true;
|
||||
window.sessionStorage.setItem(LOGIN_REDIRECT_MARK_KEY, String(Date.now()));
|
||||
window.location.href = buildAdminLoginUrl(window.location.href);
|
||||
};
|
||||
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import config from "@/config";
|
||||
import {
|
||||
clearAdminLoginRedirectMark,
|
||||
clearPersistedAuth,
|
||||
getStoredAccessToken,
|
||||
redirectToAdminLogin,
|
||||
refreshPersistedAuth,
|
||||
shouldSkipRepeatedAdminLoginRedirect,
|
||||
} from "@/api/auth";
|
||||
|
||||
export interface ApiResponse<T = any> {
|
||||
@ -76,6 +78,7 @@ const retryRequestWithFreshToken = async (requestConfig?: AuthRetryConfig) => {
|
||||
|
||||
const redirectAfterAuthExpired = () => {
|
||||
clearPersistedAuth();
|
||||
if (shouldSkipRepeatedAdminLoginRedirect()) return;
|
||||
redirectToAdminLogin();
|
||||
};
|
||||
|
||||
@ -99,6 +102,7 @@ request.interceptors.response.use(
|
||||
return Promise.reject(new Error(getAuthErrorMessage(response.data)));
|
||||
}
|
||||
|
||||
clearAdminLoginRedirectMark();
|
||||
return response.data;
|
||||
},
|
||||
async (error) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user