diff --git a/apps/web-antdv-next/src/router/guard.ts b/apps/web-antdv-next/src/router/guard.ts index bba9c4d..3290d7b 100644 --- a/apps/web-antdv-next/src/router/guard.ts +++ b/apps/web-antdv-next/src/router/guard.ts @@ -20,12 +20,6 @@ function decodeRedirect(redirect?: string) { } } -function redirectToExternalUrl(url: string) { - if (!/^https?:\/\//i.test(url)) return false; - window.location.href = url; - return true; -} - function isExternalUrl(url: string) { return /^https?:\/\//i.test(url); } @@ -84,14 +78,10 @@ function setupAccessGuard(router: Router) { preferences.app.defaultHomePath; if (isExternalUrl(redirectPath)) { - const hasCookieSession = - await authStore.ensureExternalRedirectSession(); - - if (!hasCookieSession) return true; + authStore.redirectToExternalWithAuth(redirectPath); + return false; } - if (redirectToExternalUrl(redirectPath)) return false; - return redirectPath; } return true; @@ -154,7 +144,10 @@ function setupAccessGuard(router: Router) { } else { redirectPath = to.fullPath; } - if (redirectToExternalUrl(redirectPath)) return false; + if (isExternalUrl(redirectPath)) { + authStore.redirectToExternalWithAuth(redirectPath); + return false; + } return { ...router.resolve(redirectPath), diff --git a/apps/web-antdv-next/src/store/auth.ts b/apps/web-antdv-next/src/store/auth.ts index 8e1190e..e4c077c 100644 --- a/apps/web-antdv-next/src/store/auth.ts +++ b/apps/web-antdv-next/src/store/auth.ts @@ -10,13 +10,7 @@ import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores'; import { notification } from 'antdv-next'; import { defineStore } from 'pinia'; -import { - getAccessCodesApi, - getUserInfoApi, - loginApi, - logoutApi, - refreshTokenApi, -} from '#/api'; +import { getAccessCodesApi, getUserInfoApi, loginApi, logoutApi } from '#/api'; import { $t } from '#/locales'; export const useAuthStore = defineStore('auth', () => { @@ -49,12 +43,40 @@ export const useAuthStore = defineStore('auth', () => { | string; } + function buildExternalAuthRedirectUrl(target: string) { + if (!accessStore.accessToken) return target; + + try { + const url = new URL(target); + url.searchParams.set('ktAccessToken', accessStore.accessToken); + + if (accessStore.accessCodes.length > 0) { + url.searchParams.set( + 'ktAccessCodes', + JSON.stringify(accessStore.accessCodes), + ); + } + + if (userStore.userInfo) { + url.searchParams.set('ktUserInfo', JSON.stringify(userStore.userInfo)); + } + + return url.toString(); + } catch { + return target; + } + } + + function redirectToExternalWithAuth(target: string) { + window.location.href = buildExternalAuthRedirectUrl(target); + } + async function goToRedirect(fallbackPath: string) { const redirect = decodeRedirect(getRedirectQuery() || undefined); const target = redirect || fallbackPath; if (/^https?:\/\//i.test(target)) { - window.location.href = target; + redirectToExternalWithAuth(target); return; } @@ -154,30 +176,6 @@ export const useAuthStore = defineStore('auth', () => { 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() { loginLoading.value = false; } @@ -185,9 +183,9 @@ export const useAuthStore = defineStore('auth', () => { return { $reset, authLogin, - ensureExternalRedirectSession, fetchUserInfo, loginLoading, logout, + redirectToExternalWithAuth, }; });