docs/content/docs/plugins/multi-session.mdx
The multi-session plugin allows users to maintain multiple active sessions across different accounts in the same browser. This plugin is useful for applications that require users to switch between multiple accounts without logging out.
```ts title="auth.ts"
import { betterAuth } from "better-auth"
import { multiSession } from "better-auth/plugins" // [!code highlight]
export const auth = betterAuth({
plugins: [
multiSession(), // [!code highlight]
]
})
```
Add the client plugin and Specify where the user should be redirected if they need to verify 2nd factor
```ts title="auth-client.ts"
import { createAuthClient } from "better-auth/client"
import { multiSessionClient } from "better-auth/client/plugins" // [!code highlight]
export const authClient = createAuthClient({
plugins: [
multiSessionClient() // [!code highlight]
]
})
```
Whenever a user logs in, the plugin will add additional cookie to the browser. This cookie will be used to maintain multiple sessions across different accounts.
To list all active sessions for the current user, you can call the listDeviceSessions method.
To set the active session, you can call the setActive method.
To revoke a session, you can call the revoke method.
When a user logs out, the plugin will revoke all active sessions for the user. You can do this by calling the existing signOut method, which handles revoking all sessions automatically.
You can specify the maximum number of sessions a user can have by passing the maximumSessions option to the plugin. By default, the plugin allows 5 sessions per device.
import { betterAuth } from "better-auth"
import { multiSession } from "better-auth/plugins"
export const auth = betterAuth({
plugins: [
multiSession({
maximumSessions: 3
})
]
})