diff --git a/web/src/api/http.ts b/web/src/api/http.ts index fa5fbaf..e2d7862 100644 --- a/web/src/api/http.ts +++ b/web/src/api/http.ts @@ -55,7 +55,7 @@ http.interceptors.response.use( } if (error.response?.status === 401 && !authenticationRequest && !error.config?.skipAuthSession) { clearAuthSession() - goLogin(location.pathname + location.search + location.hash) + goLogin() } return Promise.reject(error) }, diff --git a/web/src/main.ts b/web/src/main.ts index e36232e..29e0608 100644 --- a/web/src/main.ts +++ b/web/src/main.ts @@ -11,7 +11,7 @@ import { initializeAppUpdates } from './services/appUpdates' import { initializeNativeHome } from './services/nativeHome' import { initializePwa } from './services/pwa' import { initializeNativeAppLinks } from './services/nativeAppLinks' -import { setRouter } from './utils/navigate' +import { goLogin, setRouter } from './utils/navigate' import { initializeAuthSession } from './auth/session' const app = createApp(App) @@ -19,8 +19,7 @@ app.use(createPinia()) app.use(router) setRouter(router) window.addEventListener('mingxu-session-expired', () => { - const returnUrl = location.pathname + location.search + location.hash - void router.push({ name: 'login', query: { redirect: returnUrl } }) + goLogin() }) initializeAuthSession() app.mount('#app') diff --git a/web/src/utils/navigate.ts b/web/src/utils/navigate.ts index 195de15..d790da5 100644 --- a/web/src/utils/navigate.ts +++ b/web/src/utils/navigate.ts @@ -1,17 +1,53 @@ import type { Router } from 'vue-router' let router: Router | null = null +let loginNavigationPending = false + +const defaultAuthenticatedRoute = '/dashboard' + +function browserRoute(): string { + if (import.meta.env.VITE_ROUTER_MODE === 'hash') { + const hashRoute = window.location.hash.replace(/^#/, '') + return hashRoute.startsWith('/') ? hashRoute : '/' + } + return window.location.pathname + window.location.search + window.location.hash +} + +export function safeAuthRedirect(value: unknown): string { + if (typeof value !== 'string') return defaultAuthenticatedRoute + const path = value.trim() + if (!path.startsWith('/') || path.startsWith('//')) return defaultAuthenticatedRoute + + const pathOnly = path.split(/[?#]/, 1)[0].replace(/\/+$/, '').toLowerCase() + if (pathOnly === '/login') return defaultAuthenticatedRoute + + return path +} export function setRouter(r: Router): void { router = r } -export function goLogin(redirect: string): void { +export function goLogin(redirect?: unknown): void { if (router) { - router.push({ name: 'login', query: { redirect } }) - } else { - window.location.assign( - `/?redirect=${encodeURIComponent(redirect)}#/login`, + if (router.currentRoute.value.name === 'login' || loginNavigationPending) return + + const currentRoute = router.currentRoute.value + const returnUrl = safeAuthRedirect( + redirect ?? (currentRoute.name ? currentRoute.fullPath : browserRoute()), ) + loginNavigationPending = true + void router.push({ name: 'login', query: { redirect: returnUrl } }) + .finally(() => { + loginNavigationPending = false + }) + return } + + const returnUrl = safeAuthRedirect(redirect ?? browserRoute()) + const query = `redirect=${encodeURIComponent(returnUrl)}` + const loginUrl = import.meta.env.VITE_ROUTER_MODE === 'hash' + ? `${import.meta.env.BASE_URL}#/login?${query}` + : `/login?${query}` + window.location.assign(loginUrl) } diff --git a/web/src/views/LoginView.vue b/web/src/views/LoginView.vue index 71503ab..bab7445 100644 --- a/web/src/views/LoginView.vue +++ b/web/src/views/LoginView.vue @@ -6,6 +6,7 @@ import { useAuthStore } from '../stores/auth' import http from '../api/http' import { isNativeApp } from '../auth/session' import { beginNativeSsoLogin, openSsoLogin } from '../services/nativeAppLinks' +import { safeAuthRedirect } from '../utils/navigate' import LoginCaptcha from '../components/LoginCaptcha.vue' import { branding } from '../config/branding' @@ -33,7 +34,7 @@ async function submit() { twoFactorTicket.value = result.twoFactorTicket return } - await router.replace(String(route.query.redirect ?? '/dashboard')) + await router.replace(safeAuthRedirect(route.query.redirect)) } catch (error) { captchaCode.value = '' await captcha.value?.refresh() @@ -47,7 +48,7 @@ async function submitTotp() { loading.value = true try { await auth.completeTotpLogin(twoFactorTicket.value, form.totpCode) - await router.replace(String(route.query.redirect ?? '/dashboard')) + await router.replace(safeAuthRedirect(route.query.redirect)) } catch (error) { ElMessage.error(apiErrorMessage(error)) } finally { @@ -58,7 +59,7 @@ async function submitTotp() { async function startSso() { ssoLoading.value = true const apiBaseUrl = String(import.meta.env.VITE_API_BASE_URL ?? '/api').replace(/\/$/, '') - const redirect = String(route.query.redirect ?? '/dashboard') + const redirect = safeAuthRedirect(route.query.redirect) const nativeState = beginNativeSsoLogin() try { if (!captchaId.value || !captchaCode.value) return diff --git a/web/src/views/SsoBindView.vue b/web/src/views/SsoBindView.vue index 823c44f..2da424a 100644 --- a/web/src/views/SsoBindView.vue +++ b/web/src/views/SsoBindView.vue @@ -3,6 +3,7 @@ import { onMounted, reactive, ref } from 'vue' import { useRoute, useRouter } from 'vue-router' import http, { apiErrorMessage } from '../api/http' import { useAuthStore } from '../stores/auth' +import { safeAuthRedirect } from '../utils/navigate' const route = useRoute() const router = useRouter() @@ -15,11 +16,6 @@ const binding = reactive({ }) const form = reactive({ userName: '', password: '' }) -function safeRedirect(value: unknown) { - const path = String(value ?? '') - return path.startsWith('/') && !path.startsWith('//') ? path : '/dashboard' -} - async function submit() { if (!form.userName.trim() || !form.password) { ElMessage.warning('请填写现有平台账号和密码。') @@ -30,7 +26,7 @@ async function submit() { try { await auth.bindSso(String(route.query.code ?? ''), form.userName, form.password) ElMessage.success('账户绑定成功。') - await router.replace(safeRedirect(route.query.redirect)) + await router.replace(safeAuthRedirect(route.query.redirect)) } catch (error) { ElMessage.error(apiErrorMessage(error)) } finally { diff --git a/web/src/views/SsoCallbackView.vue b/web/src/views/SsoCallbackView.vue index 0bb5f83..3a3d491 100644 --- a/web/src/views/SsoCallbackView.vue +++ b/web/src/views/SsoCallbackView.vue @@ -3,16 +3,12 @@ import { onMounted } from 'vue' import { useRoute, useRouter } from 'vue-router' import { apiErrorMessage } from '../api/http' import { useAuthStore } from '../stores/auth' +import { safeAuthRedirect } from '../utils/navigate' const route = useRoute() const router = useRouter() const auth = useAuthStore() -function safeRedirect(value: unknown) { - const path = String(value ?? '') - return path.startsWith('/') && !path.startsWith('//') ? path : '/dashboard' -} - onMounted(async () => { const code = String(route.query.code ?? '') if (!code) { @@ -22,7 +18,7 @@ onMounted(async () => { try { await auth.exchangeSso(code) - await router.replace(safeRedirect(route.query.redirect)) + await router.replace(safeAuthRedirect(route.query.redirect)) } catch (error) { ElMessage.error(apiErrorMessage(error)) await router.replace({ name: 'login', query: { ssoError: 'authentication_failed' } })