其他社交提供商

Better Auth 提供了开箱即用的 通用 OAuth 插件 支持,允许你使用任何实现了 OAuth2 协议或 OpenID Connect (OIDC) 流程的社交提供商。

要使用不受开箱即用支持的提供商,你可以使用 通用 OAuth 插件

安装

将插件添加到你的 auth 配置中

要使用通用 OAuth 插件,将其添加到你的 auth 配置中。

auth.ts
import { betterAuth } from "better-auth"
import { genericOAuth } from "better-auth/plugins"
 
export const auth = betterAuth({
    // ... 其他配置选项
    plugins: [
        genericOAuth({ 
            config: [ 
                { 
                    providerId: "provider-id", 
                    clientId: "test-client-id", 
                    clientSecret: "test-client-secret", 
                    discoveryUrl: "https://auth.example.com/.well-known/openid-configuration", 
                    // ... 其他配置选项
                }, 
                // 根据需要添加更多提供商
            ] 
        }) 
    ]
})

添加客户端插件

在你的认证客户端实例中包含通用 OAuth 客户端插件。

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

阅读更多关于通用 OAuth 插件的安装和使用信息 这里

使用示例

Slack 示例

auth.ts
import { betterAuth } from "better-auth";
import { genericOAuth } from "better-auth/plugins";
 
export const auth = betterAuth({
  // ... 其他配置选项
  plugins: [
    genericOAuth({
      config: [
        {
          providerId: "slack",
          clientId: process.env.SLACK_CLIENT_ID as string,
          clientSecret: process.env.SLACK_CLIENT_SECRET as string,
          authorizationUrl: "https://slack.com/oauth/v2/authorize",
          tokenUrl: "https://slack.com/api/oauth.v2.access",
          scopes: ["users:read", "users:read.email"], // 以及其他...
        },
      ],
    }),
  ],
});
sign-in.ts
const response = await authClient.signIn.oauth2({
  providerId: "slack",
  callbackURL: "/dashboard", // 用户认证后重定向的路径
});

Instagram 示例

auth.ts
import { betterAuth } from "better-auth";
import { genericOAuth } from "better-auth/plugins";
 
export const auth = betterAuth({
  // ... 其他配置选项
  plugins: [
    genericOAuth({
      config: [
        {
          providerId: "instagram",
          clientId: process.env.INSTAGRAM_CLIENT_ID as string,
          clientSecret: process.env.INSTAGRAM_CLIENT_SECRET as string,
          authorizationUrl: "https://api.instagram.com/oauth/authorize",
          tokenUrl: "https://api.instagram.com/oauth/access_token",
          scopes: ["user_profile", "user_media"],
        },
      ],
    }),
  ],
});
sign-in.ts
const response = await authClient.signIn.oauth2({
  providerId: "instagram",
  callbackURL: "/dashboard", // 用户认证后重定向的路径
});

Coinbase 示例

auth.ts
import { betterAuth } from "better-auth";
import { genericOAuth } from "better-auth/plugins";
 
export const auth = betterAuth({
  // ... 其他配置选项
  plugins: [
    genericOAuth({
      config: [
        {
          providerId: "coinbase",
          clientId: process.env.COINBASE_CLIENT_ID as string,
          clientSecret: process.env.COINBASE_CLIENT_SECRET as string,
          authorizationUrl: "https://www.coinbase.com/oauth/authorize",
          tokenUrl: "https://api.coinbase.com/oauth/token",
          scopes: ["wallet:user:read"], // 以及其他...
        },
      ],
    }),
  ],
});
sign-in.ts
const response = await authClient.signIn.oauth2({
  providerId: "coinbase",
  callbackURL: "/dashboard", // 用户认证后重定向的路径
});

On this page