初次提交
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { useAuth } from '~/composables/useAuth'
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||
const { isAuthenticated, initAuth } = useAuth()
|
||||
const publicRoutes = ['/login', '/', '/auth/error', '/register']
|
||||
|
||||
// 客户端初始化认证状态
|
||||
if (import.meta.client && !isAuthenticated.value) {
|
||||
await initAuth()
|
||||
}
|
||||
|
||||
// 公共页面跳过认证
|
||||
if (publicRoutes.includes(to.path) || to.path.startsWith('/api/auth')) {
|
||||
return
|
||||
}
|
||||
|
||||
// 服务端跳过认证检查
|
||||
if (import.meta.server) {
|
||||
return
|
||||
}
|
||||
|
||||
// 未认证用户重定向到登录页
|
||||
if (!isAuthenticated.value && to.path !== '/login') {
|
||||
// 保存目标路径用于登录后重定向
|
||||
const redirect = to.fullPath
|
||||
return navigateTo(`/login?redirect=${encodeURIComponent(redirect)}`)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user