Docs

Multi Session

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.

Installation

Add the plugin to your auth config

auth.ts
import { betterAuth } from "better-auth"
import { multiSession } from "better-auth/plugins"
 
export const auth = betterAuth({
    plugins: [ 
        multiSession(), 
    ] 
})

Add the client Plugin

Add the client plugin and Specify where the user should be redirected if they need to verify 2nd factor

client.ts
import { createAuthClient } from "better-auth/client"
import { multiSessionClient } from "better-auth/client/plugins"
 
const client = createAuthClient({
    plugins: [
        multiSessionClient()
    ]
})

Usage

Whenver 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.

List all device sessions

To list all active sessions for the current user, you can call the listDeviceSessions method.

await authClient.multiSession.listDeviceSessions()

on the server you can call listDeviceSessions method.

await auth.api.listDeviceSessions()

Set active session

To set the active session, you can call the setActive method.

await authClient.multiSession.setActive({
    sessionId: "session-id"
})

Revoke a session

To revoke a session, you can call the revoke method.

await authClient.multiSession.revoke({
    sessionId: "session-id"
})

Revoke all sessions

To revoke all sessions, you can call the existing signOut method.

await authClient.signOut()

Options

Max Sessions

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.

auth.ts
import { betterAuth } from "better-auth"
 
export const auth = betterAuth({
    plugins: [
        multiSession({
            maximumSessions: 3
        })
    ]
})

On this page

Edit on GitHub