mirror of
https://github.com/KwiTsukasa/kt-template-admin.git
synced 2026-05-27 16:35:47 +08:00
fix(admin): 回传外部授权态
This commit is contained in:
parent
cb4aebccdd
commit
46452a5117
@ -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) {
|
function isExternalUrl(url: string) {
|
||||||
return /^https?:\/\//i.test(url);
|
return /^https?:\/\//i.test(url);
|
||||||
}
|
}
|
||||||
@ -84,14 +78,10 @@ function setupAccessGuard(router: Router) {
|
|||||||
preferences.app.defaultHomePath;
|
preferences.app.defaultHomePath;
|
||||||
|
|
||||||
if (isExternalUrl(redirectPath)) {
|
if (isExternalUrl(redirectPath)) {
|
||||||
const hasCookieSession =
|
authStore.redirectToExternalWithAuth(redirectPath);
|
||||||
await authStore.ensureExternalRedirectSession();
|
return false;
|
||||||
|
|
||||||
if (!hasCookieSession) return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (redirectToExternalUrl(redirectPath)) return false;
|
|
||||||
|
|
||||||
return redirectPath;
|
return redirectPath;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -154,7 +144,10 @@ function setupAccessGuard(router: Router) {
|
|||||||
} else {
|
} else {
|
||||||
redirectPath = to.fullPath;
|
redirectPath = to.fullPath;
|
||||||
}
|
}
|
||||||
if (redirectToExternalUrl(redirectPath)) return false;
|
if (isExternalUrl(redirectPath)) {
|
||||||
|
authStore.redirectToExternalWithAuth(redirectPath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...router.resolve(redirectPath),
|
...router.resolve(redirectPath),
|
||||||
|
|||||||
@ -10,13 +10,7 @@ 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 {
|
import { getAccessCodesApi, getUserInfoApi, loginApi, logoutApi } from '#/api';
|
||||||
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', () => {
|
||||||
@ -49,12 +43,40 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
| string;
|
| 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) {
|
async function goToRedirect(fallbackPath: string) {
|
||||||
const redirect = decodeRedirect(getRedirectQuery() || undefined);
|
const redirect = decodeRedirect(getRedirectQuery() || undefined);
|
||||||
const target = redirect || fallbackPath;
|
const target = redirect || fallbackPath;
|
||||||
|
|
||||||
if (/^https?:\/\//i.test(target)) {
|
if (/^https?:\/\//i.test(target)) {
|
||||||
window.location.href = target;
|
redirectToExternalWithAuth(target);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,30 +176,6 @@ 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;
|
||||||
}
|
}
|
||||||
@ -185,9 +183,9 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
return {
|
return {
|
||||||
$reset,
|
$reset,
|
||||||
authLogin,
|
authLogin,
|
||||||
ensureExternalRedirectSession,
|
|
||||||
fetchUserInfo,
|
fetchUserInfo,
|
||||||
loginLoading,
|
loginLoading,
|
||||||
logout,
|
logout,
|
||||||
|
redirectToExternalWithAuth,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user