Docs

Anonymous

The Anonymous plugin allows users to have an authenticated experience without requiring them to provide an email address, password, OAuth provider, or any other Personally Identifiable Information (PII). Users can later link an authentication method to their account when ready.

Installation

Add the plugin to your auth config

To enable anonymous authentication, add the anonymous plugin to your authentication configuration.

auth.ts
import { betterAuth } from "better-auth"
import { anonymous } from "better-auth/plugins"
 
export const auth = betterAuth({
    // ... other config options
    plugins: [
        anonymous() 
    ]
})

Migrate the database

Run the migration or generate the schema to add the necessary fields and tables to the database.

npx @better-auth/cli migrate

See the Schema section to add the fields manually.

Add the client plugin

Next, include the anonymous client plugin in your authentication client instance.

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

Usage

Sign In

To sign in a user anonymously, use the signIn.anonymous() method.

example.ts
const user = await client.signIn.anonymous()

Once the user is signed in, you can link an account to their anonymous profile. Currently, only email/password linking is supported.

example.ts
const user = await client.anonymous.linkAccount({
    email: "user@email.com",
    password: "secure-password"
})

Options

  • emailDomainName: The domain name to use when generating an email address for anonymous users. Defaults to the domain name of the current site.
auth.ts
import { betterAuth } from "better-auth"
 
export const auth = betterAuth({
    plugins: [
        anonymous({
            emailDomainName: "example.com"
        })
    ]
})

Schema

The anonymous plugin requires an additional field in the user table:

Field NameTypeKeyDescription
isAnonymous
boolean
Indicates whether the user is anonymous.

On this page

Edit on GitHub