N
Nuxt2y ago
Cesar

Global middleware infinite redirection

Hi, I can't get my middleware to work to block access to pages if the user is not logged in. I have an infinite loop that I can't explain. This is my global middleware code: import { useAuthStore } from "~/stores/auth" export default defineNuxtRouteMiddleware(() => { const authStore = useAuthStore() if (!Object.keys({...authStore.user}).length) { console.log('not authenticated') return navigateTo('/login') } })
4 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Cesar
CesarOP2y ago
Thank's for the answer but not working for me :/ What is the name of this file in your case? On my side it is called auth.global.ts While making the modifications I have the same problem, it passes 10 times in it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Cesar
CesarOP2y ago
Ahhhhh it's working but in my case I need it everywhere It's working with global when i add test on from + to export default defineNuxtRouteMiddleware(async (_to, _from) => { const authStore = useAuthStore() const isAuthenticated = !!Object.keys(authStore.user).length const loginPath = '/login' if (!isAuthenticated && _from.path !== loginPath && _to.path !== loginPath) { return await navigateTo("/login") } }) Thank's ! 🙂
Want results from more Discord servers?
Add your server