Integrate authentication in your scene
This section explains how to set up your scene to integrate an authentication layer to communicate with Unity Cloud services.
Prerequisites
- Follow the Installation instructions.
- Follow the Get started instructions.
- Follow the Best practices: dependency injection guide.
Overview
To integrate authentication in your scene, perform the following procedures:
- Instantiate a
CompositeAuthenticatorinPlatformServices. - Tie Identity's features with a UI.
- Leverage the access token to communicate with other Unity Cloud services.
Instantiate a CompositeAuthenticator in PlatformServices
To instantiate a CompositeAuthenticator in the PlatformServices class, follow these steps:
- Use the
CompositeAuthenticatorSettingsBuilderto build a prioritized list of supportedIAuthenticator. - Create an instance of the
CompositeAuthenticatorusing theCompositeAuthenticatorSettingsas argument. - Add the following references of the created instance of the
CompositeAuthenticator: - A private read-only reference
- A public reference as
ICompositeAuthenticator,IAuthenticationStateProviderandIAccessTokenProvider - Initialize the
CompositeAuthenticatorand any other services in theInitializeAsyncmethod. - Shutdown the
CompositeAuthenticatorand any other services in theShutdownmethod.
Tie the CompositeAuthenticator with UI
If your application needs to support the interactive login flow, create a login UI and link it to the IUrlRedirectionAuthenticator methods to achieve interactive login and logout.
Please note that if the CompositeAuthenticatorSettings added automated flow or preauthenticated flow IAuthenticator, it's not guaranteed that the activated flow is interactive. This means some parts of the UI need to be hidden or disabled from code, depending on the situation. Validate this by checking the value of the ICompositeAuthenticator.RequiresGUI property when managing UI element.
- Create Login and Logout buttons in your scene.
- Create a LoginManager GameObject and attach a new LoginManager MonoBehaviour to it. This script links your UI with Identity's authentication engine.
- Update the
LoginManagerscript so it references your buttons and anIAuthenticationStateProvider. - Update the
Awakemethod to do the following:- Retrieve the
CompositeAuthenticatorreference fromPlatformServices. - Subscribe to the
AuthenticationStateChangedevent. - Subscribe to the buttons'
onClickevents. - Call
ApplyAuthenticationStateto update the UI when the scene loads.
- Retrieve the
- Make sure the subscriptions are cleaned up in
OnDestroy. - Implement the
ApplyAuthenticationStatemethod to update your buttons based on the current authentication state. Make your buttons interactive in specific circumstances, otherwise you risk errors and exceptions. To determine if you should display your buttons, rely on theAuthenticationStateand theRequiresGUIproperty that determines whether the UI is relevant in the selected authentication flow. - Define the behaviors for your buttons by calling the appropriate methods.
- Test your scene to see how the buttons update along with the authentication state of the user.
Note: Only an interactive login flow (like the one exposed by theIUrlRedirectionAuthenticatorinterface) requires these methods. If the flow isn't interactive, callingLoginAsyncorLogoutAsyncthrows exceptions.
Leverage the access token to communicate with other Unity Cloud services
After you set up your login flow, communication with other Unity Cloud services is possible. All Unity Cloud services expect an instance of IAccessTokenProvider, which the PlatformServices provides.
For an example of a service that expects an IAccessTokenProvider, refer to the Get user information guide.