Social Login (OAuth)
Create a script with the following code and bind it to an object.
The SDK exports a method called AuthenticateWithOAuth
that implements the pooling OAuth flow. The method takes an OAuthInitRequest
object as a parameter. The object contains the provider, the redirect URL, and the pooling option.
_30using System.Collections;_30using System.Collections.Generic;_30using UnityEngine;_30using Openfort.OpenfortSDK;_30using Openfort.OpenfortSDK.Model;_30_30public class InitOpenfort : MonoBehaviour_30{_30 private OpenfortSDK openfort;_30_30 async void Start()_30 {_30 string publishableKey = "YOUR_OPENFORT_PUBLISHABLE_KEY";_30 openfort = await OpenfortSDK.Init(publishableKey);_30 }_30_30 public async void OnGoogleClicked()_30 {_30 OAuthInitRequest request = new OAuthInitRequest()_30 {_30 Provider = OAuthProvider.FACEBOOK,_30 UsePooling = false,_30 Options = new OAuthInitRequestOptions()_30 {_30 RedirectTo = "mygame://callback"_30 },_30 };_30 await openfort.AuthenticateWithOAuth(request);_30 }_30}
Openfort is now accessible from anywhere via Openfort.Instance.
Standalone Windows applications do not support window deep linking from a web browser to the application. Modify the request above to:
_10OAuthInitRequest request = new OAuthInitRequest()_10 {_10 Provider = OAuthProvider.FACEBOOK,_10 };
Android, iOS and macOS#
Android setup#
On Android, we utilize Chrome Custom Tabs (if available) to seamlessly connect gamers to Passport from within the game.
UNITY VERSIONS BELOW 2021.3
To check if Chrome Custom Tabs are available, older Unity versions may require a specific Gradle plugin version. For example, on Unity 2019.4, you must upgrade from 3.4.* to 3.4.3 (see Android's blog post):
- In Unity go to Build Settings -> Player Settings -> Android -> Publishing Settings -> Enable Custom Base Gradle Template under the Build section
- Open the newly generated Assets/Plugins/Android/baseProjectTemplate.grade file
- Update classpath
com.android.tools.build:gradle:3.4.0
to classpathcom.android.tools.build:gradle:3.4.3
- In Unity go to Build Settings -> Player Settings -> Android -> Publishing Settings -> Enable Custom Main Manifest and Custom Main Gradle Template under the Build section
- Open the newly generated
Assets/Plugins/Android/AndroidManifest.xml
file. Add the following code inside the<application>
element:
_10<activity_10 android:name="com.openfort.unity.RedirectActivity"_10 android:exported="true" >_10 <intent-filter android:autoVerify="true">_10 <action android:name="android.intent.action.VIEW" />_10 <category android:name="android.intent.category.DEFAULT" />_10 <category android:name="android.intent.category.BROWSABLE" />_10 <data android:scheme="mygame" android:host="callback" />_10 </intent-filter>_10</activity>
- Open the newly generated
Assets/Plugins/Android/mainTemplate.gradle
file. Add the following code insidedependencies
block:
For this version of the Chrome Custom Tabs to work, the compileSdkVersion must be at least 33. This is usually the same value as the targetSdkVersion, which you can set in Build Settings -> Player Settings -> Android -> Other Settings -> Target API Level.
Proguard
If you enable Minify in your project settings, you will need to add a custom Proguard file to your project.
- In Unity go to Build Settings -> Player Settings -> Android -> Publishing Settings -> Enable Custom Proguard File under the Build section
- Open the newly generated Assets/Plugins/Android/proguard-user.txt file. Add the following code inside the
<application>
element
_10-dontwarn com.openfort.**_10-keep class com.openfort.** { *; }_10-keep interface com.openfort.** { *; }_10_10-dontwarn androidx.**_10-keep class androidx.** { *; }_10-keep interface androidx.** { *; }
The application will now open when the device processes any link that starts with mygame://callback
.
iOS setup#
- In Unity go to Build Settings -> Player Settings -> iOS -> Other Settings -> Supported URL schemes.
- Increment the Size number.
- Add your URL scheme in the Element field, e.g. if the deeplink URL is
mygame://callback
, add the schememygame
to the field.
Uppon successful authentication, the SDK will return a token that can be used to authenticate the user in your application.