匿名

Anonymous 插件允许用户在无需提供电子邮件地址、密码、OAuth 提供商或任何其他个人身份信息(PII)的情况下进行身份验证体验。用户可以在准备好时,将认证方法链接到他们的账户。

【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】

将插件添加到你的认证配置

要启用匿名身份验证,请将匿名插件添加到你的身份验证配置中。

auth.ts
import { betterAuth } from "better-auth"
import { anonymous } from "better-auth/plugins"

export const auth = betterAuth({
    // ... other config options
    plugins: [
        anonymous() 
    ]
})

迁移数据库

运行迁移或生成架构,以向数据库添加必要的字段和表。

npx @better-auth/cli migrate
npx @better-auth/cli generate

See the Schema section to add the fields manually.

添加客户端插件

接下来,在你的身份验证客户端实例中包含匿名客户端插件。

auth-client.ts
import { createAuthClient } from "better-auth/client"
import { anonymousClient } from "better-auth/client/plugins"

export const authClient = createAuthClient({
    plugins: [
        anonymousClient()
    ]
})

用法

【Usage】

登录

【Sign In】

要以匿名方式登录用户,请使用 signIn.anonymous() 方法。

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

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

【Link Account】

如果用户已经以匿名身份登录,并尝试使用其他方式进行 signInsignUp,他们的匿名活动可以关联到新账户。

【If a user is already signed in anonymously and tries to signIn or signUp with another method, their anonymous activities can be linked to the new account.】

要做到这一点,你首先需要向插件提供 onLinkAccount 回调。

【To do that you first need to provide onLinkAccount callback to the plugin.】

auth.ts
import { betterAuth } from "better-auth"

export const auth = betterAuth({
    plugins: [
        anonymous({
            onLinkAccount: async ({ anonymousUser, newUser }) => {
               // perform actions like moving the cart items from anonymous user to the new user
            }
        })
    ]

然后,当你使用其他方法调用 signInsignUp 时,onLinkAccount 回调将被触发。同时,anonymousUser 将被默认删除。

【Then when you call signIn or signUp with another method, the onLinkAccount callback will be called. And the anonymousUser will be deleted by default.】

example.ts
const user = await authClient.signIn.email({
    email,
})

删除匿名用户

【Delete Anonymous User】

要删除匿名用户,你可以调用 /delete-anonymous-user 接口。

【To delete an anonymous user, you can call the /delete-anonymous-user endpoint.】

POST
/delete-anonymous-user
await authClient.deleteAnonymousUser();

注意事项:

  • 当匿名用户的账户链接到新的认证方式时,匿名用户会默认被删除。
  • disableDeleteAnonymousUser 设置为 true 可以防止匿名用户调用 /delete-anonymous-user 接口。

选项

【Options】

emailDomainName

用于为匿名用户生成电子邮件地址的域名。如果未提供,将使用默认格式 temp@{id}.com

【The domain name to use when generating an email address for anonymous users. If not provided, the default format temp@{id}.com is used.】

auth.ts
import { betterAuth } from "better-auth"

export const auth = betterAuth({
    plugins: [
        anonymous({
            emailDomainName: "example.com" // -> temp-{id}@example.com
        })
    ]
})

generateRandomEmail

用于为匿名用户生成电子邮件地址的自定义函数。这样可以让你定义自己的电子邮件格式。该函数可以是同步的,也可以是异步的。

【A custom function to generate email addresses for anonymous users. This allows you to define your own email format. The function can be synchronous or asynchronous.】

auth.ts
import { betterAuth } from "better-auth"

export const auth = betterAuth({
    plugins: [
        anonymous({
            generateRandomEmail: () => { 
                const id = crypto.randomUUID() 
                return `guest-${id}@example.com`
            } 
        })
    ]
})

注意事项:

  • 如果提供了 generateRandomEmail,则会忽略 emailDomainName
  • 你有责任确保邮箱是唯一的,以避免冲突。返回的邮箱必须是有效格式。

onLinkAccount

当匿名用户将其账户关联到新的身份验证方法时会调用的回调函数。该回调接收一个包含 anonymousUsernewUser 的对象。

【A callback function that is called when an anonymous user links their account to a new authentication method. The callback receives an object with the anonymousUser and the newUser.】

disableDeleteAnonymousUser

默认情况下,当匿名用户将其账户链接到新的身份验证方法时,匿名用户记录会被自动删除。如果将此选项设置为 true,此自动删除功能将被禁用,并且 /delete-anonymous-user 端点将不再对匿名用户可访问。

【By default, when an anonymous user links their account to a new authentication method, the anonymous user record is automatically deleted. If you set this option to true, this automatic deletion will be disabled, and the /delete-anonymous-user endpoint will no longer be accessible to anonymous users.】

generateName

一个回调函数,用于为匿名用户生成名称。如果你希望为匿名用户使用随机名称,或者如果 name 在你的数据库中是唯一的,这将非常有用。

【A callback function that is called to generate a name for the anonymous user. Useful if you want to have random names for anonymous users, or if name is unique in your database.】

架构

【Schema】

匿名插件需要在用户表中添加一个额外字段:

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

Field NameTypeKeyDescription
isAnonymousbooleanIndicates whether the user is anonymous.

On this page