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, 
        }, 
    },
})

用法

【Usage】

使用 Vercel 登录

【Sign in with Vercel】

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

【To sign in with Vercel, you can use the signIn.social function provided by the client. The signIn function takes an object with the following properties:】

  • 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"
    })
}

选项

【Options】

有关所有社交提供商支持的完整选项列表,请查阅 提供商选项

【For the full list of options supported by all social providers, check the Provider Options.】

可用范围

【Available Scopes】

Vercel 支持以下 OpenID Connect 范围:

【Vercel supports the following OpenID Connect scopes:】

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

作用域在 Vercel 应用上配置,因此不需要向提供者传递 scope 参数,省略它会更方便。

【Scopes are configured on the Vercel App, and so it is not necessary to pass a scope parameter to the provider and it is more convenient to omit it.】

如果你确实传递了 scope 参数,它的效果是请求已配置作用域的一个子集:

【If you do pass a scope parameter, it has the effect of requesting a subset of the configured scopes:】

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 文档

On this page