new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Better Auth · Plugins · all subjects

bearer plugin

10 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Bearer plugin installation

To install the Bearer plugin, import it from 'better-auth/plugins' and add it to the plugins array in the betterAuth configuration. Example: import { bearer } from 'better-auth/plugins'; export const auth = betterAuth({ plugins: [bearer()] });

Bearer token authentication purpose

The Bearer plugin enables authentication using Bearer tokens as an alternative to browser cookies. It intercepts requests and adds the Bearer token to the Authorization header before forwarding them to your API. It is intended only for APIs that do not support cookies or require Bearer tokens for authentication.

Obtaining Bearer token from sign-in response

After a successful sign-in, the Bearer token is received in the response headers with the key 'set-auth-token'. It can be accessed via ctx.response.headers.get('set-auth-token') in the onSuccess callback.

Storing Bearer token securely

The Bearer token should be stored securely, such as in localStorage using localStorage.setItem('bearer_token', authToken) or similar secure storage mechanisms.

Configure auth client with Bearer token

Set up the auth client to include the Bearer token in all requests by configuring fetchOptions with auth type 'Bearer' and a token function that retrieves the token from storage: createAuthClient({ fetchOptions: { auth: { type: 'Bearer', token: () => localStorage.getItem('bearer_token') || '' } } }).

Per-request Bearer token authorization

Bearer tokens can be provided for individual requests by passing fetchOptions with the Authorization header: await authClient.listSessions({ fetchOptions: { headers: { Authorization: `Bearer ${token}` } } }).

Using Bearer token for API calls outside auth client

Bearer tokens can authenticate any request to an API outside of the auth client by including the token in the Authorization header: fetch('https://api.example.com/data', { headers: { Authorization: `Bearer ${token}` } }).

Server-side session validation with Bearer token

On the server, requests can be authenticated using auth.api.getSession() by passing the request headers that contain the Authorization Bearer token header. If no session is found, the request is unauthorized.

Bearer plugin requireSignature option

The Bearer plugin accepts a requireSignature option (boolean) to require the token to be signed. Default value is false.

Bearer plugin security warning

The Bearer plugin should be used cautiously as it is intended only for APIs that do not support cookies or require Bearer tokens. Improper implementation could easily lead to security vulnerabilities.

Give your agent this brain