Skip to content

Commit ae2cf03

Browse files
docs: redirect loop warn (#1487)
* avoid infinite redirect * chore: review --------- Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent 67dd21c commit ae2cf03

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/nuxt/auth.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ definePageMeta({
5454
</script>
5555
```
5656

57+
::: warning
58+
59+
If you are using a [global middleware](https://nuxt.com/docs/getting-started/routing#route-middleware), make sure **you are not getting into a redirect loop** by ensuring `navigateTo()` is only called if the target location is not the same page:
60+
61+
```ts{4}
62+
// middleware/auth.global.ts
63+
export default defineNuxtRouteMiddleware(async (to, from) => {
64+
// ...
65+
if (!user && to.path !== '/login') {
66+
return navigateTo({ path: '/login' })
67+
}
68+
})
69+
```
70+
71+
:::
72+
73+
````vue{2-4}
74+
5775
You can even automatically handle the auth state by _watching_ the current user. We recommend you do this in either a layout or the `app.vue` component so the watcher is always active:
5876
5977
```vue

0 commit comments

Comments
 (0)