Files
BYW-VoiceHub/VoiceHub-main/app/middleware/auth.global.ts
T
Henry_Du 4ce4649aaf
Build VoiceHub FPK / build-fpk (push) Failing after 56m5s
Build and Push Docker Image / build-and-push (push) Canceled after 1h9m35s
初次提交
2026-09-06 08:19:02 +08:00

29 lines
792 B
TypeScript

import { useAuth } from '~/composables/useAuth'
export default defineNuxtRouteMiddleware(async (to, from) => {
const { isAuthenticated, initAuth } = useAuth()
const publicRoutes = ['/login', '/', '/auth/error']
// 客户端初始化认证状态
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)}`)
}
})