Skip to content

Commit

Permalink
Merge pull request #14 from casualmatt/main
Browse files Browse the repository at this point in the history
feat: Middleware example to docs
  • Loading branch information
Intevel committed Apr 4, 2022
2 parents 92d29a5 + 67caccb commit 97312c5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/content/en/usage/useDirectusAuth.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,29 @@ const onSubmit = async () => {
};
</script>
```

## Middleware example
### Redirect user to login
You can protect your authenticated routes by creating a custom middleware in your project, here is an example:

Create ```./middleware/auth.ts```

```ts
export default defineNuxtRouteMiddleware((to, _from) => {
const user = useDirectusUser();

if (!user.value) {
return navigateTo('/login')
}
})
```

Now you can add the middleware to your pages

```ts
<script setup lang="ts">
definePageMeta({
middleware: ["auth"]
})
</script>
```

0 comments on commit 97312c5

Please sign in to comment.