🐛 Fix the authorize page

This commit is contained in:
2025-09-30 01:04:20 +08:00
parent 888d3cb5ba
commit 98a5d34f3c
6 changed files with 2895 additions and 30 deletions

26
app/middleware/auth.ts Normal file
View File

@@ -0,0 +1,26 @@
import { useUserStore } from '~/stores/user'
export default defineNuxtRouteMiddleware(async to => {
const userStore = useUserStore()
// If the user is already authenticated, we're good.
if (userStore.isAuthenticated) {
return
}
// If we have a token, but no user object, fetch the user.
// This happens on initial load or page refresh.
if (userStore.token && !userStore.user) {
await userStore.fetchUser()
}
// If the user is still not authenticated, redirect to login.
if (!userStore.isAuthenticated) {
// Abort navigation to the original page.
if (to.path !== '/auth/login') {
const redirect = encodeURIComponent(to.fullPath)
console.log(`[AUTH] Redirecting to /auth/login?redirect=${redirect}`)
return navigateTo(`/auth/login?redirect=${redirect}`)
}
}
})