其他社交提供商

Better Auth 提供对任何通过 Generic OAuth 插件 实现 OAuth2 协议或 OpenID Connect (OIDC) 流程的社交提供商的支持。你可以使用针对 Auth0、Keycloak、Okta、Microsoft Entra ID 和 Slack 等流行提供商的预配置辅助函数,或手动配置任何 OAuth 提供商。

🌐 Better Auth provides support for any social provider that implements the OAuth2 protocol or OpenID Connect (OIDC) flows through the Generic OAuth Plugin. You can use pre-configured helper functions for popular providers like Auth0, Keycloak, Okta, Microsoft Entra ID, and Slack, or manually configure any OAuth provider.

安装

🌐 Installation

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

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

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

export const auth = betterAuth({
    // ... other config options
    plugins: [
        genericOAuth({ 
            config: [ 
                { 
                    providerId: "provider-id", 
                    clientId: "test-client-id", 
                    clientSecret: "test-client-secret", 
                    discoveryUrl: "https://auth.example.com/.well-known/openid-configuration", 
                    // ... other config options
                }, 
                // Add more providers as needed
            ] 
        }) 
    ]
})

添加客户端插件

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

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

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

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

示例用法

🌐 Example Usage

这是配置通用 OAuth 提供程序的基本示例:

🌐 Here's a basic example of configuring a generic OAuth provider:

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

export const auth = betterAuth({
  plugins: [
    genericOAuth({
      config: [
        {
          providerId: "provider-id",
          clientId: process.env.CLIENT_ID,
          clientSecret: process.env.CLIENT_SECRET,
          discoveryUrl: "https://auth.example.com/.well-known/openid-configuration",
        },
      ],
    }),
  ],
})

使用预配置的提供者

🌐 Using Pre-configured Providers

Better Auth 为流行的 OAuth 提供商提供了预配置的辅助函数。下面是一个使用 Slack 的示例:

🌐 Better Auth provides pre-configured helper functions for popular OAuth providers. Here's an example using Slack:

auth.ts
import { betterAuth } from "better-auth"
import { genericOAuth, slack } from "better-auth/plugins"

export const auth = betterAuth({
  plugins: [
    genericOAuth({
      config: [
        slack({
          clientId: process.env.SLACK_CLIENT_ID,
          clientSecret: process.env.SLACK_CLIENT_SECRET,
        }),
      ],
    }),
  ],
})
sign-in.ts
const response = await authClient.signIn.oauth2({
  providerId: "slack",
  callbackURL: "/dashboard",
})

有关更多预配置的提供商(Auth0、Keycloak、Okta、Microsoft Entra ID)及其配置选项,请参阅通用 OAuth 插件文档

🌐 For more pre-configured providers (Auth0, Keycloak, Okta, Microsoft Entra ID) and their configuration options, see the Generic OAuth Plugin documentation.

手动配置示例

🌐 Manual Configuration Examples

如果你需要配置一个没有预配置助手的提供者,你可以手动进行配置:

🌐 If you need to configure a provider that doesn't have a pre-configured helper, you can manually configure it:

Instagram 示例

🌐 Instagram Example

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

export const auth = betterAuth({
  // ... other config options
  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", // the path to redirect to after the user is authenticated
});

Coinbase 示例

🌐 Coinbase Example

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

export const auth = betterAuth({
  // ... other config options
  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"], // and more...
        },
      ],
    }),
  ],
});
sign-in.ts
const response = await authClient.signIn.oauth2({
  providerId: "coinbase",
  callbackURL: "/dashboard", // the path to redirect to after the user is authenticated
});

On this page