Skip to content

Commit f519bbc

Browse files
committed
fix: Enhances auth middleware and improves logging
Improves the authentication middleware by preventing redirection loops on the login page and adding more informative logging. The auth middleware now checks if the requested path is '/login' and returns early, preventing an infinite redirection loop. Also, the log message now includes the path from which the user is being redirected, which gives a better understanding of user flow.
1 parent 113b165 commit f519bbc

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/runtime/plugin.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
1313
console.warn('[Nuxt Users] ⚠️ Migrations table does not exist, you should run the migration script to create it by running: npx nuxt-users migrate')
1414
}
1515

16-
addRouteMiddleware('auth.global', (to: RouteLocationNormalized, _from: RouteLocationNormalized) => {
16+
addRouteMiddleware('auth.global', (to: RouteLocationNormalized, from: RouteLocationNormalized) => {
17+
if (to.path === '/login') {
18+
return
19+
}
20+
1721
const { user } = useAuth()
1822

1923
// TODO add role based access control see #55
2024
if (
2125
user.value || nuxtUsers.auth?.whitelist?.includes(to.path)
2226
) {
23-
console.log('🔐 [Nuxt Users] Auth middleware: Access allowed')
27+
console.log('[Nuxt Users] 🔐 Auth middleware: Access allowed')
2428
return
2529
}
2630

27-
console.log('🔐 [Nuxt Users] Auth middleware: Redirecting to login - no user and not whitelisted')
31+
console.log(`[Nuxt Users] 🔐 Auth middleware: Redirecting from ${from.path} to login - no user and not whitelisted`)
2832
return navigateTo('/login')
2933
},
3034
{ global: true })

0 commit comments

Comments
 (0)