🐛 Fix middleware of auth

This commit is contained in:
2025-10-02 01:16:47 +08:00
parent 6853acea98
commit 4327083e03

View File

@@ -1,6 +1,6 @@
import { useUserStore } from '~/stores/user'
import { useUserStore } from "~/stores/user"
export default defineNuxtRouteMiddleware(async to => {
export default defineNuxtRouteMiddleware(async (to) => {
const userStore = useUserStore()
// If the user is already authenticated, we're good.
@@ -10,14 +10,14 @@ export default defineNuxtRouteMiddleware(async to => {
// 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) {
if (!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') {
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}`)