frontend layout and infrastructure
This commit is contained in:
+35
-1
@@ -13,9 +13,43 @@ const router = createRouter({
|
||||
routes,
|
||||
})
|
||||
|
||||
const pinia = createPinia()
|
||||
const app = createApp(App)
|
||||
|
||||
app.use(createPinia())
|
||||
app.use(pinia)
|
||||
app.use(router)
|
||||
|
||||
// 路由守卫
|
||||
const authWhitelist = ['/login', '/register']
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
const { useUserStore } = await import('./stores/user')
|
||||
const userStore = useUserStore()
|
||||
|
||||
// 白名单页面(登录/注册)
|
||||
if (authWhitelist.includes(to.path)) {
|
||||
if (userStore.isLoggedIn) {
|
||||
return '/'
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 未登录跳转登录页
|
||||
if (!userStore.isLoggedIn) {
|
||||
return `/login?redirect=${to.fullPath}`
|
||||
}
|
||||
|
||||
// 已登录但未获取用户信息
|
||||
if (!userStore.user) {
|
||||
try {
|
||||
await userStore.fetchCurrentUser()
|
||||
} catch {
|
||||
userStore.logout()
|
||||
return '/login'
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
app.mount('#app')
|
||||
|
||||
Reference in New Issue
Block a user