App Logo

Discord

Guide on how to set up Discord authentication using Authula.

Note: This guide uses Discord OAuth2 for user sign-in, which is recommended by Discord for new integrations.

Obtain Discord credentials

  1. Go to the Discord Developer Portal.
  2. Click on "New Application" and provide a name for your application.
  3. Fill in all the general information required for your application.
  4. Navigate to the "OAuth2" section and then you will see your "Client ID" and "Client Secret". Make a note of the "Client ID" and "Client Secret". Bear in mind you will only be shown the "Client Secret" once.
  5. Under "OAuth2", go to the "Redirects" section and add the following redirect URI:
    # Assuming base path is /api/auth
    http://localhost:8080/api/auth/oauth2/callback/discord
    For production, replace localhost with the base URL of your Authula server (e.g. https://api.yourdomain.com).

Configure the provider

Standalone Mode

Add the following to your config.toml:

# SECURITY NOTE: It is recommended to set the 'client_secret' for each of these via their
# respective environment variables as shown in the .env.example file rather than hardcoding it here.
[plugins.oauth2.providers.discord]
enabled = true
client_id = "your-client-id"
client_secret = "your-client-secret"
redirect_url = "http://localhost:8080/auth/oauth2/callback/discord"
scopes = []

Library Mode

import (
  "os"
  "fmt"

  authula "github.com/Authula/authula"
  authulaconfig "github.com/Authula/authula/config"
  authulamodels "github.com/Authula/authula/models"
  oauth2plugin "github.com/Authula/authula/plugins/oauth2"
  oauth2plugintypes "github.com/Authula/authula/plugins/oauth2/types"
)

func main() {
  config := authulaconfig.NewConfig(
    authulaconfig.WithSecurity(authulamodels.SecurityConfig{
      // Make sure to configure CORS and Trusted Origins appropriately
      TrustedOrigins: []string{"your frontend origin"},
      CORS: authulamodels.CORSConfig{
        AllowCredentials: true,
        AllowedOrigins:   []string{"your frontend origin"},
      },
    }),
  )
  auth := authula.New(&authula.AuthConfig{
    Config:  config,
    Plugins: []authulamodels.Plugin{
      oauth2plugin.New(oauth2plugintypes.OAuth2PluginConfig{
        Enabled: true,
        Providers: map[string]oauth2plugintypes.ProviderConfig{
          "discord": {
            Enabled:      true,
            ClientID:     os.Getenv(authulaenv.EnvDiscordClientID),
            ClientSecret: os.Getenv(authulaenv.EnvDiscordClientSecret),
            RedirectURL:  fmt.Sprintf("%s%s/oauth2/callback/discord", config.BaseURL, config.BasePath),
          },
        },
      }),
    },
  })
}

Test the integration

  1. Start your Authula server.
  2. In your webapp, create a button when clicked on, it navigates the user to:
    # (replace localhost with your Authula server URL in production).
    http://localhost:8080/auth/oauth2/authorize/discord?redirect_to=<YOUR_REDIRECT_URL>
  3. You should be redirected to Discord's authorization page. After authorizing, you will be redirected back to your application.

On this page