mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-06 12:27:44 +00:00
feat: session and user management docs
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -196,6 +196,40 @@ export const contents: Content[] = [
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "Session Management",
|
||||
href: "/docs/concepts/session-management",
|
||||
icon: () => (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="1.2em"
|
||||
height="1.2em"
|
||||
viewBox="0 0 20 20"
|
||||
>
|
||||
<path
|
||||
className="fill-foreground"
|
||||
d="M16 5c0 1.657-2.686 3-6 3S4 6.657 4 5s2.686-3 6-3s6 1.343 6 3m-1.31 3.016a6 6 0 0 0 .81-.485c0 .811-.696 1.439-1.412 1.821a3 3 0 0 0-.815 4.658A2.5 2.5 0 0 0 11 16.5c0 .485.106.974.33 1.426Q10.687 18 10 18c-3.314 0-6-1.343-6-3V7.12c.383.362.84.661 1.31.896C6.562 8.642 8.222 9 10 9s3.438-.358 4.69-.984M17.5 12a2 2 0 1 1-4 0a2 2 0 0 1 4 0m1.5 4.5c0 1.245-1 2.5-3.5 2.5S12 17.75 12 16.5a1.5 1.5 0 0 1 1.5-1.5h4a1.5 1.5 0 0 1 1.5 1.5"
|
||||
></path>
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "User Management",
|
||||
href: "/docs/concepts/user-management",
|
||||
icon: () => (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="1.2em"
|
||||
height="1.2em"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
className="fill-foreground"
|
||||
d="M17 15q-1.05 0-1.775-.725T14.5 12.5t.725-1.775T17 10t1.775.725t.725 1.775t-.725 1.775T17 15m-4 5q-.425 0-.712-.288T12 19v-.4q0-.6.313-1.112t.887-.738q.9-.375 1.863-.562T17 16t1.938.188t1.862.562q.575.225.888.738T22 18.6v.4q0 .425-.288.713T21 20zm-3-8q-1.65 0-2.825-1.175T6 8t1.175-2.825T10 4t2.825 1.175T14 8t-1.175 2.825T10 12m-8 5.2q0-.85.425-1.562T3.6 14.55q1.5-.75 3.113-1.15T10 13q.875 0 1.75.15t1.75.35l-1.7 1.7q-.625.625-1.213 1.275T10 18v.975q0 .3.113.563t.362.462H4q-.825 0-1.412-.587T2 18z"
|
||||
></path>
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
],
|
||||
Icon: () => (
|
||||
<svg
|
||||
|
||||
81
docs/content/docs/concepts/session-management.mdx
Normal file
81
docs/content/docs/concepts/session-management.mdx
Normal file
@@ -0,0 +1,81 @@
|
||||
---
|
||||
title: Session Management
|
||||
description: Better Auth Session Management
|
||||
---
|
||||
|
||||
Better auth manages session using a traditional cookie-based session management. The session is stored in a cookie and is sent to the server on every request. The server then verifies the session and returns the user data if the session is valid.
|
||||
|
||||
## Session table
|
||||
|
||||
The session table stores the session data. The session table has the following fields:
|
||||
|
||||
- `id`: The session id. Which is also used as the session cookie.
|
||||
- `userId`: The user id of the user.
|
||||
- `expiresAt`: The expiration date of the session.
|
||||
- `ipAddress`: The IP address of the user.
|
||||
- `userAgent`: The user agent of the user. It stores the user agent header from the request.
|
||||
|
||||
## Session Expiration
|
||||
|
||||
The session expires after 7 days by default. But whenever the session is used, and the `updateAge` is reached the session expiration is updated to the current time plus the `expiresIn` value.
|
||||
|
||||
You can change both the `expiresIn` and `updateAge` values by passing the `session` object to the `auth` configuration.
|
||||
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth"
|
||||
|
||||
export const auth = await betterAuth({
|
||||
//... other config options
|
||||
session: {
|
||||
expiresIn: 1000 * 60 * 60 * 24 * 7 // 7 days,
|
||||
updateAge: 1000 * 60 * 60 * 24 // 1 day (every 1 day the session expiration is updated)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Session Management
|
||||
|
||||
Better Auth provides a set of functions to manage sessions.
|
||||
|
||||
### List Sessions
|
||||
|
||||
The `listSessions` function returns a list of sessions that are active for the user.
|
||||
|
||||
```ts title="client.ts"
|
||||
import { client } from "@/lib/client"
|
||||
|
||||
const sessions = await client.user.listSessions()
|
||||
```
|
||||
|
||||
### Revoke Session
|
||||
|
||||
When a user signs out of a device, the session is automatically ended. However, you can also end a session manually from any device the user is signed into.
|
||||
|
||||
To end a session, use the `revokeSession` function. Just pass the session ID as a parameter.
|
||||
|
||||
```ts title="client.ts"
|
||||
await client.user.revokeSession({
|
||||
id: session.id,
|
||||
})
|
||||
```
|
||||
|
||||
### Revoke All Sessions
|
||||
|
||||
To revoke all sessions, you can use the `revokeSessions` function.
|
||||
|
||||
```ts title="client.ts"
|
||||
await client.user.revokeSessions()
|
||||
```
|
||||
|
||||
### Revoking Sessions on Password Change
|
||||
|
||||
You can revoke all sessions when the user changes their password by passing `revokeOtherSessions` true on `changePAssword` function.
|
||||
|
||||
```ts title="auth.ts"
|
||||
await user.changePassword({
|
||||
newPassword: newPassword,
|
||||
currentPassword: currentPassword,
|
||||
revokeOtherSessions: signOutDevices,
|
||||
})
|
||||
```
|
||||
|
||||
44
docs/content/docs/concepts/user-management.mdx
Normal file
44
docs/content/docs/concepts/user-management.mdx
Normal file
@@ -0,0 +1,44 @@
|
||||
---
|
||||
title: User Management
|
||||
description: User management concepts
|
||||
---
|
||||
|
||||
Beyond authenticating users, Better auth also provides a set of functions to manage users. This includes, updating user information, changing passwords, and more.
|
||||
|
||||
## User table
|
||||
|
||||
The user table stores the user data. The user table has the following fields:
|
||||
|
||||
- `id`: The user id.
|
||||
- `email`: The email of the user.
|
||||
- `name`: The name of the user.
|
||||
- `image`: The image of the user.
|
||||
- `createdAt`: The creation date of the user.
|
||||
- `updatedAt`: The last update date of the user.
|
||||
|
||||
The user table can be extended by plugins to store additional data. When a plugin extends a user table it's infered by the type system and can be used in the client.
|
||||
|
||||
## Update User
|
||||
|
||||
### Update User Information
|
||||
|
||||
To update user information, you can use the `updateUser` function provided by the client. The `updateUser` function takes an object with the following properties:
|
||||
|
||||
```ts
|
||||
await user.update({
|
||||
image: "https://example.com/image.jpg",
|
||||
name: "John Doe",
|
||||
})
|
||||
```
|
||||
|
||||
### Change Password
|
||||
|
||||
Password of a user isn't stored in the user table. Instead, it's stored in the account table. To change the password of a user, you can use the `changePassword` function provided by the client. The `changePassword` function takes an object with the following properties:
|
||||
|
||||
```ts
|
||||
await user.changePassword({
|
||||
newPassword: "newPassword123",
|
||||
currentPassword: "oldPassword123",
|
||||
revokeOtherSessions: true, // revoke all other sessions the user is signed into
|
||||
});
|
||||
```
|
||||
Reference in New Issue
Block a user