Polar

Polar 提供者的设置和使用。

获取 Polar 凭证

要使用 Polar 登录,您需要创建一个 OAuth 2.0 客户端。您可以从 Polar 用户设置 获取凭证。

  1. 进入 Polar 用户设置

  2. 点击“创建 OAuth 客户端”

  3. 填写所需字段:

    • 应用名称:授权期间显示给用户的名称
    • 客户端类型:为您的应用选择适当的客户端类型
    • 重定向 URI:本地开发设置为 http://localhost:3000/api/auth/callback/polar。生产环境设置为 https://yourdomain.com/api/auth/callback/polar。如果修改了认证路由的基础路径,请相应更新重定向 URI。
    • 范围:选择您的应用所需的权限(openid、profile、email 为默认值)
    • 主页 URL:应用的主页地址
  4. 可选配置:

    • 您的应用 Logo
    • 服务条款 URL
    • 隐私政策 URL
  5. 创建完成后,将客户端 ID 和客户端密钥复制到环境变量中。请妥善保管这些凭证。

配置提供者

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

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

export const auth = betterAuth({
    socialProviders: {
        polar: { 
            clientId: process.env.POLAR_CLIENT_ID as string, 
            clientSecret: process.env.POLAR_CLIENT_SECRET as string, 
        }, 
    },
})

使用 Polar 登录

要使用 Polar 登录,您可以使用客户端提供的 signIn.social 函数,其中 provider 应设置为 polar。

auth-client.ts
import { createAuthClient } from "better-auth/client"
const authClient = createAuthClient()

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