Overview
This guide explains how to authenticate users with their existing Discord accounts via OAuth2, enabling seamless login and access to Discord features.Flexible account options
If a player does not have a Discord account, you can use the SDK to create a provisional account so they can still access your game’s social features. See Using provisional accounts for implementation details.Prerequisites
Before you begin, make sure you have:- Read the Core Concepts guide to understand OAuth2 authentication, Discord application setup, and SDK initialization
- Created a Discord application in the Developer Portal
- Downloaded and configured the Discord Social SDK
- A working basic SDK integration (initialization and connection)
At a minimum, you need the
openid sdk.social_layer_presence scopes for account linking and presence features. If you need lobbies, voice chat, or direct messaging, use openid sdk.social_layer instead. See OAuth2 scopes for the full breakdown.Authentication flow
OAuth2 is the standard authentication flow that allows users to sign in using their Discord account. The process follows these steps:User approval
The SDK opens a browser window, Discord client, or in-game overlay to prompt the user to approve the request.
Receive authorization code
After approval, Discord redirects the user back to your app with an authorization code.
The OAuth2 flow requires a user’s account to be verified.
OAuth2 using the Discord Social SDK
- If the Discord client has overlay support (Windows only), the OAuth2 login modal appears in your game instead of opening a browser.
- The SDK automatically handles redirects, simplifying the authentication flow.
- CSRF protection and other security measures are built in, but you should always follow best practices to secure your app.
Requesting access tokens
Step 0: Configure OAuth2 redirects
Register the correct redirect URIs for your app in the Discord Developer Portal.| Platform | Redirect URI |
|---|---|
| Desktop | http://127.0.0.1/callback |
| Mobile | discord-APP_ID:/authorize/callback (replace APP_ID with your Discord application ID) |
Step 1: Request authorization
UseClient::Authorize to initiate authorization.
Authorization scopes
| Helper method | Scopes requested | Features enabled |
|---|---|---|
Client::GetDefaultPresenceScopes | openid sdk.social_layer_presence | Account linking, friends list, rich presence |
Client::GetDefaultCommunicationScopes | openid sdk.social_layer | All of the above, plus lobbies, voice chat, direct messaging, and linked channels |
Client::GetDefaultPresenceScopes unless you know you need the communication features.
Authorization code verifier
If you are usingClient::GetToken for the token exchange, you must include a PKCE code challenge. Use Client::CreateAuthorizationCodeVerifier to generate the code challenge and verifier pair.
Step 2: User approval
After callingClient::Authorize, the SDK opens a browser window, Discord client, or in-game overlay for the user to approve the request.
Step 3: Receiving the authorization code
Once the user approves, Discord redirects back to your app with an authorization code in thecode parameter of the callback.
Step 4: Exchanging the authorization code for an access token
- Server-to-server (confidential client)
- Client-side (public client)
If your application uses a backend server and does not have Public Client enabled, exchange the authorization code server-side:Example response:
Working with tokens
Once you have an access token, pass it to the SDK usingClient::UpdateToken, then call Client::Connect:
Refreshing access tokens
Access tokens expire after 7 days. Use the refresh token to obtain a new access token without requiring the user to re-authorize.- Server-to-server (confidential client)
- Client-side (public client)
Revoking access tokens
When a user disconnects their Discord account or a token is compromised, revoke the tokens to invalidate them.- Server-to-server (confidential client)
- Client-side (public client)
Handling user-initiated revocation
Users can unlink their account by removing access to your application from their Discord User Settings > Authorized Apps page. To be notified when a user unlinks this way, configure your application to listen for theAPPLICATION_DEAUTHORIZED webhook event. Otherwise, you’ll know the user has unlinked because their access token and refresh token will be invalidated on the next request.
Next steps
Creating a unified friends list
Combine Discord and game friends into a single list for easy management.
Setting rich presence
Display game status and information to Discord friends.
Using provisional accounts
Allow players without Discord accounts to access social features.
Change log
| Date | Changes |
|---|---|
| March 17, 2025 | Initial release |