修复redirect
This commit is contained in:
1 parent
23fdbf422f
commit
01fc72629b
6 files changed
+52
-24
No files matched your search
+1
-1
@@ -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)
|
||||
},
|
||||
|
||||
+2
-3
@@ -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')
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Loaded 3 of 6 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user