Other Social Providers

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

Add the plugin to your auth config

To use the Generic OAuth plugin, add it to your auth config.

config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
plugins: [
BetterAuth::Plugins.generic_oauth(
  config: [
    {
      provider_id: "custom-provider",
      client_id: ENV.fetch("CUSTOM_PROVIDER_CLIENT_ID"),
      client_secret: ENV.fetch("CUSTOM_PROVIDER_CLIENT_SECRET"),
      authorization_url: "https://provider.example.com/oauth/authorize",
      token_url: "https://provider.example.com/oauth/token",
      user_info_url: "https://provider.example.com/oauth/userinfo",
      scopes: ["openid", "email", "profile"]
    }
  ]
)
]
)

Add the client plugin

Include the Generic OAuth client plugin in your authentication client instance.

config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
plugins: [
BetterAuth::Plugins.generic_oauth(
  config: [
    {
      provider_id: "custom-provider",
      client_id: ENV.fetch("CUSTOM_PROVIDER_CLIENT_ID"),
      client_secret: ENV.fetch("CUSTOM_PROVIDER_CLIENT_SECRET"),
      authorization_url: "https://provider.example.com/oauth/authorize",
      token_url: "https://provider.example.com/oauth/token",
      user_info_url: "https://provider.example.com/oauth/userinfo",
      scopes: ["openid", "email", "profile"]
    }
  ]
)
]
)

Read more about installation and usage of the Generic Oauth plugin here.

Example Usage

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

config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
  secret: ENV.fetch("BETTER_AUTH_SECRET"),
  base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
  plugins: [
    BetterAuth::Plugins.generic_oauth(
      config: [
        {
          provider_id: "custom-provider",
          client_id: ENV.fetch("CUSTOM_PROVIDER_CLIENT_ID"),
          client_secret: ENV.fetch("CUSTOM_PROVIDER_CLIENT_SECRET"),
          authorization_url: "https://provider.example.com/oauth/authorize",
          token_url: "https://provider.example.com/oauth/token",
          user_info_url: "https://provider.example.com/oauth/userinfo",
          scopes: ["openid", "email", "profile"]
        }
      ]
    )
  ]
)

Using Pre-configured Providers

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

config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
  secret: ENV.fetch("BETTER_AUTH_SECRET"),
  base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
  plugins: [
    BetterAuth::Plugins.generic_oauth(
      config: [
        BetterAuth::Plugins.slack(
          client_id: ENV.fetch("SLACK_CLIENT_ID"),
          client_secret: ENV.fetch("SLACK_CLIENT_SECRET")
        )
      ]
    )
  ]
)
sign-in.rb
response = auth.api.sign_in_with_oauth2(
  body: {
    provider_id: "slack",
    callback_url: "/dashboard",
    disable_redirect: true
  }
)

redirect_url = response.fetch(:url)

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 Example

config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
  secret: ENV.fetch("BETTER_AUTH_SECRET"),
  base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
  plugins: [
    BetterAuth::Plugins.generic_oauth(
      config: [
        {
          provider_id: "instagram",
          client_id: ENV.fetch("INSTAGRAM_CLIENT_ID"),
          client_secret: ENV.fetch("INSTAGRAM_CLIENT_SECRET"),
          authorization_url: "https://provider.example.com/oauth/authorize",
          token_url: "https://provider.example.com/oauth/token",
          user_info_url: "https://provider.example.com/oauth/userinfo",
          scopes: ["openid", "email", "profile"]
        }
      ]
    )
  ]
)
sign-in.rb
response = auth.api.sign_in_with_oauth2(
  body: {
    provider_id: "instagram",
    callback_url: "/dashboard",
    disable_redirect: true
  }
)

redirect_url = response.fetch(:url)

Coinbase Example

config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
  secret: ENV.fetch("BETTER_AUTH_SECRET"),
  base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
  plugins: [
    BetterAuth::Plugins.generic_oauth(
      config: [
        {
          provider_id: "coinbase",
          client_id: ENV.fetch("COINBASE_CLIENT_ID"),
          client_secret: ENV.fetch("COINBASE_CLIENT_SECRET"),
          authorization_url: "https://provider.example.com/oauth/authorize",
          token_url: "https://provider.example.com/oauth/token",
          user_info_url: "https://provider.example.com/oauth/userinfo",
          scopes: ["openid", "email", "profile"]
        }
      ]
    )
  ]
)
sign-in.rb
response = auth.api.sign_in_with_oauth2(
  body: {
    provider_id: "coinbase",
    callback_url: "/dashboard",
    disable_redirect: true
  }
)

redirect_url = response.fetch(:url)

On this page