docs: improve bearer token docs

This commit is contained in:
Bereket Engida
2024-10-10 09:33:50 +03:00
parent 5209a8133e
commit a0f3429846

View File

@@ -1,11 +1,13 @@
---
title: Bearer
description: The Bearer plugin allows you to authenticate with a Bearer token instead of a browser cookie.
title: Bearer Token Authentication
description: Authenticate API requests using Bearer tokens instead of browser cookies
---
The Bearer plugin allows you to authenticate with a Bearer token instead of a browser cookie. It proxies the request and set the bearer token in the Authorization header to a cookie request internally.
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.
## Add the Bearer plugin
## Installing the Bearer Plugin
Add the Bearer plugin to your authentication setup:
```ts title="auth.ts"
import { betterAuth } from "better-auth";
@@ -14,4 +16,95 @@ import { bearer } from "better-auth/plugins";
export const auth = betterAuth({
plugins: [bearer()]
});
```
## How to Use Bearer Tokens
### 1. Obtain the Bearer Token
After a successful sign-in, you'll receive a session object containing the session object. The `id` is the token you need to send in the Authorization header for all subsequent requests.
```ts title="client.ts"
const { data } = await authClient.signIn.email({
email: "user@example.com",
password: "securepassword"
});
const token = data.session.id;
// Store the token securely (e.g., in localStorage)
localStorage.setItem("bearer_token", token);
```
### 2. Configure the Auth Client
Set up your auth client to include the Bearer token in all requests:
```ts title="auth-client.ts"
const token = localStorage.getItem("bearer_token");
export const authClient = createAuthClient({
fetchOptions: {
headers: {
Authorization: `Bearer ${token}`
}
}
});
```
### 3. Make Authenticated Requests
Now you can make authenticated API calls:
```ts title="client.ts"
// This request is automatically authenticated
const { data } = await authClient.user.listSessions();
```
### 4. Per-Request Token (Optional)
You can also provide the token for individual requests:
```ts title="client.ts"
const { data } = await authClient.user.listSessions({
fetchOptions: {
headers: {
Authorization: `Bearer ${token}`
}
}
});
```
### 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:
```ts title="api-call.ts"
const token = localStorage.getItem("bearer_token");
const response = await fetch("https://api.example.com/data", {
headers: {
Authorization: `Bearer ${token}`
}
});
const data = await response.json();
```
And in the server, you can use the `auth.api.getSession` function to authenticate requests:
```ts title="server.ts"
import { auth } from "@/auth";
export async function handler(req, res) {
const session = await auth.api.getSession({
headers: req.headers
});
if (!session) {
return res.status(401).json({ error: "Unauthorized" });
}
// Process authenticated request
// ...
}
```