Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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