docs.unity3d.com
    Show / Hide Table of Contents

    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:

    1. Instantiate a CompositeAuthenticator in PlatformServices.
    2. Tie Identity's features with a UI.
    3. 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:

    1. Use the CompositeAuthenticatorSettingsBuilder to build a prioritized list of supported IAuthenticator.
    2. Create an instance of the CompositeAuthenticator using the CompositeAuthenticatorSettings as argument.
    3. Add the following references of the created instance of the CompositeAuthenticator:
      1. A private read-only reference
      2. A public reference as ICompositeAuthenticator, IAuthenticationStateProvider and IAccessTokenProvider
    4. Initialize the CompositeAuthenticator and any other services in the InitializeAsync method.
    5. Shutdown the CompositeAuthenticator and any other services in the Shutdown method.

    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.

    1. Create Login and Logout buttons in your scene.
    2. Creating buttons in the scene
    3. Create a LoginManager GameObject and attach a new LoginManager MonoBehaviour to it. This script links your UI with Identity's authentication engine.
    4. Update the LoginManager script so it references your buttons and an IAuthenticationStateProvider.
    5. Update the Awake method to do the following:
      1. Retrieve the CompositeAuthenticator reference from PlatformServices.
      2. Subscribe to the AuthenticationStateChanged event.
      3. Subscribe to the buttons' onClick events.
      4. Call ApplyAuthenticationState to update the UI when the scene loads.
    6. Make sure the subscriptions are cleaned up in OnDestroy.
    7. Implement the ApplyAuthenticationState method 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 the AuthenticationState and the RequiresGUI property that determines whether the UI is relevant in the selected authentication flow.
    8. Define the behaviors for your buttons by calling the appropriate methods.
    9. Note: Only an interactive login flow (like the one exposed by the IUrlRedirectionAuthenticator interface) requires these methods. If the flow isn't interactive, calling LoginAsync or LogoutAsync throws exceptions.
    10. Test your scene to see how the buttons update along with the authentication state of the user.

    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.

    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023