Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc(auth:session): add documentation about the silent auth middleware #146

Merged
merged 1 commit into from
Sep 11, 2024
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
20 changes: 19 additions & 1 deletion content/docs/authentication/session_guard.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ The auth middleware throws the [E_UNAUTHORIZED_ACCESS](https://github.com/adonis

## Getting access to the logged-in user

You may access the logged-in user instance using the `auth.user` property. The value is only available when using the `auth` middleware or if you call the `auth.authenticate` or `auth.check` methods manually.
You may access the logged-in user instance using the `auth.user` property. The value is only available when using the `auth` or `silent_auth` middleware or if you call the `auth.authenticate` or `auth.check` methods manually.

```ts
// title: Using auth middleware
Expand Down Expand Up @@ -177,6 +177,24 @@ router
})
```

### Silent auth middleware

The `silent_auth` middleware is similar to the `auth` middleware, but it does not throw an exception when the user is not authenticated. Instead, the request still continues as usual.

This middleware is useful when you want to always authenticate the user to perform some actions but do not want to block the request when the user is not authenticated.

If you plan to use this middleware, then you must register it inside the list of [router middleware](../basics/middleware.md#router-middleware-stack).

```ts
// title: start/kernel.ts
import router from '@adonisjs/core/services/router'

router.use([
// ...
() => import('app/middleware/silent_auth')
])
```

### Check if the request is authenticated
You can check if a request has been authenticated using the `auth.isAuthenticated` flag. The value of `auth.user` will always be defined for an authenticated request.

Expand Down