Discord
Get your Discord credentials
To use Discord sign in, you need a client ID and client secret. You can get them from the Discord Developer Portal.
Make sure to set the redirect URL to http://localhost:3000/api/auth/callback/discord for local development. For production, you should set it to the URL of your application. If you change the base path of the auth routes, you should update the redirect URL accordingly.
Configure the provider
To configure the provider, you need to import the provider and pass it to the social_providers option of the auth instance.
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
discord: BetterAuth::SocialProviders.discord(
client_id: ENV.fetch("DISCORD_CLIENT_ID"),
client_secret: ENV.fetch("DISCORD_CLIENT_SECRET")
)
}
)Discord returns email: null for phone-only accounts, even with the email scope granted. See Handling Providers Without Email for the recommended map_profile_to_user fallback.
Usage
Sign In with Discord
To sign in with Discord, call auth.api.sign_in_social on your Ruby auth instance. The endpoint body takes the following properties:
provider: The provider to use. It should be set todiscord.
response = auth.api.sign_in_social(
body: {
provider: "discord",
callback_url: "/dashboard",
error_callback_url: "/login",
disable_redirect: true
}
)
redirect_url = response.fetch(:url)Options
For the full list of options supported by all social providers, check the Provider Options.
Bot Permissions (Optional)
If you're using the bot scope with Discord OAuth, you can specify bot permissions using the permissions option. It can either be a bitwise value (e.g 2048 | 16384 for Send Messages and Embed Links) or a specific permission value (e.g 16384 for Embed Links).
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
discord: BetterAuth::SocialProviders.discord(
client_id: ENV.fetch("DISCORD_CLIENT_ID"),
client_secret: ENV.fetch("DISCORD_CLIENT_SECRET")
)
}
)Note: The permissions parameter only works when the bot scope is included in your OAuth2 scopes. Read more about Discord bot permissions.