Vercel

Vercel 提供者的设置与使用。

获取 Vercel 凭据

要使用 Vercel 登录,你需要一个客户端 ID 和客户端密钥。你可以通过 创建 Vercel 应用 从 Vercel 仪表板 获取它们。

请确保将重定向 URL 设置为 http://localhost:3000/api/auth/callback/vercel 用于本地开发。对于生产环境,应将其设置为应用程序的 URL。如果更改了认证路由的基本路径,需要相应地更新重定向 URL。

Vercel 需要 PKCE(用于代码交换的密钥)以提高安全性。这由 Better Auth 自动处理。

配置提供者

要配置提供者,你需要导入提供者并将其传递给认证实例的 socialProviders 选项。

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

export const auth = betterAuth({ 
    socialProviders: {
        vercel: { 
            clientId: process.env.VERCEL_CLIENT_ID as string, 
            clientSecret: process.env.VERCEL_CLIENT_SECRET as string, 
        }, 
    },
})

使用方法

使用 Vercel 登录

要使用 Vercel 登录,你可以使用客户端提供的 signIn.social 函数。signIn 函数接收一个对象,包含以下属性:

  • provider:要使用的提供者。应设置为 vercel。
auth-client.ts
import { createAuthClient } from "better-auth/client"
const authClient = createAuthClient()

const signIn = async () => {
    const data = await authClient.signIn.social({
        provider: "vercel"
    })
}

选项

有关所有社交提供者支持的完整选项列表,请查看 提供者选项。

可用作用域

Vercel 支持以下 OpenID Connect 作用域:

  • openid(默认):返回用户 ID 令牌中的唯一标识符
  • email:返回用户的电子邮件地址
  • profile:返回用户的个人资料信息(名称、头像)
  • offline_access:返回用于离线访问的刷新令牌

作用域在 Vercel 应用中配置,因此无需向提供者传递 scope 参数,省略此参数更为方便。

如果你传递了 scope 参数,则表示请求已配置作用域的子集:

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

export const auth = betterAuth({
    socialProviders: {
        vercel: {
            clientId: process.env.VERCEL_CLIENT_ID as string,
            clientSecret: process.env.VERCEL_CLIENT_SECRET as string,
            scope: ["openid", "email", "profile"], 
        },
    },
})

有关 Vercel 作用域和 API 功能的更多信息,请参阅官方 Vercel 文档。