Skip to content

Commit

Permalink
docs: redirect loop warn (#1487)
Browse files Browse the repository at this point in the history
* avoid infinite redirect

* chore: review

---------

Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
  • Loading branch information
thehouseisonfire and posva authored Jan 29, 2024
1 parent 67dd21c commit ae2cf03
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/nuxt/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ definePageMeta({
</script>
```

::: warning

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:

```ts{4}
// middleware/auth.global.ts
export default defineNuxtRouteMiddleware(async (to, from) => {
// ...
if (!user && to.path !== '/login') {
return navigateTo({ path: '/login' })
}
})
```

:::

````vue{2-4}
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:
```vue
Expand Down

0 comments on commit ae2cf03

Please sign in to comment.