fix(admin): 修复外部回跳循环

This commit is contained in:
sunlei 2026-05-17 15:57:41 +08:00
parent ff32a8111d
commit cb4aebccdd
2 changed files with 43 additions and 1 deletions

View File

@ -26,6 +26,10 @@ function redirectToExternalUrl(url: string) {
return true; return true;
} }
function isExternalUrl(url: string) {
return /^https?:\/\//i.test(url);
}
function getRedirectQuery(queryRedirect?: string) { function getRedirectQuery(queryRedirect?: string) {
if (queryRedirect) return queryRedirect; if (queryRedirect) return queryRedirect;
@ -79,6 +83,13 @@ function setupAccessGuard(router: Router) {
userStore.userInfo?.homePath || userStore.userInfo?.homePath ||
preferences.app.defaultHomePath; preferences.app.defaultHomePath;
if (isExternalUrl(redirectPath)) {
const hasCookieSession =
await authStore.ensureExternalRedirectSession();
if (!hasCookieSession) return true;
}
if (redirectToExternalUrl(redirectPath)) return false; if (redirectToExternalUrl(redirectPath)) return false;
return redirectPath; return redirectPath;

View File

@ -10,7 +10,13 @@ import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
import { notification } from 'antdv-next'; import { notification } from 'antdv-next';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { getAccessCodesApi, getUserInfoApi, loginApi, logoutApi } from '#/api'; import {
getAccessCodesApi,
getUserInfoApi,
loginApi,
logoutApi,
refreshTokenApi,
} from '#/api';
import { $t } from '#/locales'; import { $t } from '#/locales';
export const useAuthStore = defineStore('auth', () => { export const useAuthStore = defineStore('auth', () => {
@ -148,6 +154,30 @@ export const useAuthStore = defineStore('auth', () => {
return userInfo; return userInfo;
} }
async function ensureExternalRedirectSession() {
try {
const resp = (await refreshTokenApi()) as string | { data?: string };
const accessToken = typeof resp === 'string' ? resp : resp.data;
if (!accessToken) return false;
accessStore.setAccessToken(accessToken);
const [fetchUserInfoResult, accessCodes] = await Promise.all([
fetchUserInfo(),
getAccessCodesApi(),
]);
userStore.setUserInfo(fetchUserInfoResult);
accessStore.setAccessCodes(accessCodes);
accessStore.setLoginExpired(false);
return true;
} catch {
resetAllStores();
accessStore.setLoginExpired(false);
return false;
}
}
function $reset() { function $reset() {
loginLoading.value = false; loginLoading.value = false;
} }
@ -155,6 +185,7 @@ export const useAuthStore = defineStore('auth', () => {
return { return {
$reset, $reset,
authLogin, authLogin,
ensureExternalRedirectSession,
fetchUserInfo, fetchUserInfo,
loginLoading, loginLoading,
logout, logout,