Discord - hippogamesunity/SimpleSignIn GitHub Wiki
Welcome to Simple Discord Sign-In wiki!
The asset provides [Sign in with Discord] using OAuth 2.0 for Android, iOS, Windows, macOS, Universal Windows Platform (UWP) and WebGL apps made with Unity.
Benefits
- Cross-platform user auth for cross-platform games and apps
- No plugins, no 3rd party auth libs, no dependencies
- No impact to build size
- Get access tokens to make Discord API calls
- More security for client-server apps (get an access token on a client, get all user data on a server to avoid tampering)
- SFSafariViewController is used on iOS (required by App Store review)
- Deep linking for Windows (UNITY_STANDALONE_WIN)
Terminology
- Please visit Terminology section
Understanding how it works
- Your app navigates users to Discord
Authorization Endpointin a web browser - Users perform sign-in using their login and password
- Discord
Authorization Endpointredirects users toRedirect URI(which isAuthorization Middleware) and provides an authorizationcode Authorization Middlewaretransmitscodeto your app (using deep linking when possible)- The app exchanges
codeforaccess tokenand requests user info with it
Preconditions
- For Android, iOS, macOS, Windows and UWP (platforms that support deep linking): COME UP WITH your
Custom URI scheme(orProtocol). It MUST contain the period symbol.and small alphanumeric symbols only (no spaces, no undercores). In my example it issimple.oauth, but it can bejelly.bean(note thatCustom URI schemeis not the same as your actual package name or bundle id). - For Android, iOS, UWP: enable deep linking as described in Unity documentation or as described below.
- For Android: create
AndroidManifest.xmlinsideAssets/Plugins/Android/, SET yourCustom URI schemeinside, like<data android:scheme="simple.oauth" />. You can useAndroidManifestExample.xmlfrom the asset as an example, just copy, rename and edit. AGAIN, DON'T FORGET TO REPLACEsimple.oauthwith yourCustom URI scheme! - For iOS and macOS: navigate to
Player Settings > Other > Configurationand add yourCustom URI schemetoSupported URL schemes. In Xcode, make sure that the URL scheme is added (Register your URL scheme). - For Universal Windows Platform: navigate to
Player Settings > Publishing Settingsand setProtocol(it MUST contain a period symbol, for examplesimple.oauth), then enableInternetClientinCapabilities. - For Windows: navigate to
Player Settingsand enableResolution and Presentation > Force Single Instanceand setOther Settings > Api Compatibility Level = .NET Framework
Setup steps
- Visit Discord for developers / Applications, create a new account if needed
- Create a new app if needed, or use an existing app
- Navigate to
OAuth2tab and copyClient IDandClient Secret - Add
https://hippogames.dev/api/oauth/redirecttoRedirects(for Editor mode, addhttp://localhost:20561/as well) - Return to Unity and configure
Resources/DiscordAuthSettings.asset- set
Client IDandClient Secret - set
Custom URI Schemefrom Preconditions - set
Access Scopes(optional)
- set
Checklist
-
Custom URI schemeis picked, and it has a different value thansimple.oauth -
Custom URI schemeis set in 2 places: [1] Resources/DiscordAuthSettings.asset, [2] your application manifest (AndroidManifest.xml for Android,Supported URL schemesfor iOS,Protocolfor UWP) -
Resources/DiscordAuthSettings.assetcontains valid settings different from that come with the asset
Usage
- Check our
Examplescene and C# code ofExample.cs - Create an instance of
DiscordAuth - Call
DiscordAuth.SignInorDiscordAuth.GetTokenResponse - Create
OnSignInorOnGetTokenResponsecallbacks - Build and test
- Write a review on the Asset Store :)
API reference for DiscordAuth class
| Method | Arguments | Description |
|---|---|---|
| DiscordAuth (constructor) | DiscordAuthSettings settings = null | A constructor that accepts an instance of DiscordAuthSettings. If Null is passed, it will load default settings from Resources (DiscordAuthSettings scriptable object). |
| SignIn | Action<bool, string, UserInfo> callback, bool caching = true | Performs sign-in and returns an instance of UserInfo with callback. If caching is True, it will return the previously saved UserInfo. |
| SignOut | bool revokeAccessToken = false | Performs sign-out. Can revoke Access Token if requested. |
| GetTokenResponse | Action<bool, string, TokenResponse> callback | Returns an instance of TokenResponse which contains AccessToken and other related information (expiration, type and other). It may also contain IdToken (JWT), if requested with openid scope, which contains information about the user. |
| TryResume | Refer to code | This can be called on app startup to continue OAuth. In some scenarios, the app may be terminated while the user performs sign-in on Discord website. |
| SignInAsync | Async version of SignIn. | |
| GetTokenResponseAsync | Async version of GetTokenResponse. |
Best practices
- Call
DiscordAuth.SignInwithcaching: trueto return cachedUserInfo. - Call
DiscordAuth.GetTokenResponseinstead ofDiscordAuth.SignInif you need an access token only (and don't needUserInfo). - You can use
DiscordAuth.SavedAuthto getTokenResponseorUserInfo(don't forget to check all values for null). - Disable debug logs for production by setting
DiscordAuth.DebugLog = false. - Check Manual cancellation if needed.
Security concerns
- Please refer to User data disclosure.
- It's recommended to deploy your own trusted
Authorization Middlewareto handle sensitive data. Please refer to Authorization Middleware article. - We have to store
Secret Keyinside the client app. If it's important for you, you can migrate all C# code to your ASP.NET backend.
Known issues
- Please visit Common issues section.