Bearer Token Authentication
The Bearer plugin enables authentication using Bearer tokens as an alternative to browser cookies. It intercepts requests, adding the Bearer token to the Authorization header before forwarding them to your API.
Use this cautiously, only intended for APIs that doesn't support cookies or require Bearer tokens for authentication. This could easily lead to security vulnerabilities if not implemented correctly.
Installing the Bearer Plugin
Add the Bearer plugin to your authentication setup:
How to Use Bearer Tokens
1. Obtain the Bearer Token
After a successful sign-in, you'll receive a session token in the response headers. Store this token securely (e.g., in localStorage
):
You can also set this up globally in your auth client:
You may want to clear the token based on the response status code or other conditions:
2. Configure the Auth Client
Set up your auth client to include the Bearer token in all requests:
3. Make Authenticated Requests
Now you can make authenticated API calls:
4. Per-Request Token (Optional)
You can also provide the token for individual requests:
5. Using Bearer Tokens Outside the Auth Client
The Bearer token can be used to authenticate any request to your API, even when not using the auth client:
And in the server, you can use the auth.api.getSession
function to authenticate requests:
Options
requireSignature (boolean): Require the token to be signed. Default: false
.