docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use case: Get user information

    This section explains how to set up your scene to get information about your application's user.

    Getting user information is an example of a Unity Cloud service that you can call after authenticating the user in your application. This service needs an Access Token to identify the user and authorize the call.

    Before you start

    To use this sample, you must first Integrate authentication in your scene.

    How do I...?

    Create references to IAuthenticator inherited interfaces

    The CompositeAuthenticator, like all IAuthenticator, inherits IServiceAuthorizer, IAuthenticationStateProvider, IUserInfoProvider and IOrganizationRepository interfaces. Create an instance of the CompositeAuthenticator and use it to reference all inherited interfaces.

    readonly IAuthenticator m_CompositeAuthenticator;
    IServiceAuthorizer m_ServiceAuthorizer => m_CompositeAuthenticator;
    IUserInfoProvider m_UserInfoProvider => m_CompositeAuthenticator;
    IAuthenticationStateProvider m_AuthenticationStateProvider => m_CompositeAuthenticator;
    IOrganizationRepository m_OrganizationRepository => m_CompositeAuthenticator;
    

    Leverage the IUserInfoProvider in your scene

    To leverage the IUserInfoProvider in your scene, follow these steps:

    1. In your scene, create a Text field that displays the current authentication state (or the username if the user is logged in). Creating a text field in the scene

    2. Create a UserNameUpdater script and attach it to the LoginManager GameObject. This script fills the text with the correct values based on the authentication state.

    3. Update the UserNameUpdater class so it references your Text field, IAuthenticator, and IUserInfoProvider.

    [SerializeField]
    Text m_UserInfoText;
    
    ICompositeAuthenticator m_CompositeAuthenticator;
    IAuthenticationStateProvider m_AuthenticationStateProvider => m_CompositeAuthenticator;
    IUserInfoProvider m_UserInfoProvider => m_CompositeAuthenticator;
    IUserInfo m_UserInfo;
    
    1. The Awake and Destroy method should manage PlatformServices references and events. The async Start method should apply the initial authentication state.
    void Awake()
    {
        m_CompositeAuthenticator = PlatformServices.CompositeAuthenticator;
        m_AuthenticationStateProvider.AuthenticationStateChanged += OnAuthenticationStateChanged;
    }
    
    async Task Start()
    {
        await ApplyAuthenticationState(m_AuthenticationStateProvider.AuthenticationState);
    }
    
    void OnDestroy()
    {
        m_AuthenticationStateProvider.AuthenticationStateChanged -= OnAuthenticationStateChanged;
    }
    
    1. The OnAuthenticationStateChanged method updates the text based on the current authentication state, and the IUserInfoProvider.GetUserInfoAsync method can be called when the user is logged in.
    async void OnAuthenticationStateChanged(AuthenticationState state)
    {
        await ApplyAuthenticationState(state);
    }
    
    async Task ApplyAuthenticationState(AuthenticationState state)
    {
        switch (state)
        {
            case AuthenticationState.AwaitingInitialization:
            case AuthenticationState.AwaitingLogout:
            case AuthenticationState.LoggedOut:
                m_UserInfoText.text = "...";
                break;
            case AuthenticationState.AwaitingLogin:
                m_UserInfoText.text = "Awaiting completion of a user initiated manual login operation...";
                break;
            case AuthenticationState.LoggedIn:
                m_UserInfo = await m_UserInfoProvider.GetUserInfoAsync();
                BuildUserInfoText();
                break;
        }
    }
    
    void BuildUserInfoText()
    {
        var sb = new StringBuilder();
        sb.Append(m_UserInfo.Name);
        sb.Append(m_CompositeAuthenticator.RequiresGUI
            ? " is logged in with an access token issued after a successful user initiated login operation."
            : " is logged in with an access token read from the browser local storage.");
        m_UserInfoText.text = sb.ToString();
    }
    
    1. Select Play. The text updates based on the authentication state.

    Inject an IServiceAuthorizer in ServiceHttpClient to access resources on Unity Cloud

    Since all IAuthenticator inherits the IServiceAuthorizer interface, you can inject a reference from any IAuthenticator class implementation, like the CompositeAuthenticator, into the ServiceHttpClient constructor method as a valid IServiceAuthorizer.

    The ServiceHttpClient can then be injected in any class of other Unity.Cloud Unity packages to access resources on Unity Cloud.

    var httpClient = new UnityHttpClient();
    var playerSettings = UnityCloudPlayerSettings.Instance;
    var platformSupport = PlatformSupportFactory.GetAuthenticationPlatformSupport();
    var serviceHostResolver = UnityRuntimeServiceHostResolverFactory.Create();
    var compositeAuthenticatorSettings = new CompositeAuthenticatorSettingsBuilder(httpClient, platformSupport, serviceHostResolver, playerSettings)
        .AddDefaultBrowserAuthenticatedAccessTokenProvider(playerSettings)
        .AddDefaultPkceAuthenticator(playerSettings)
        .Build();
    
    // Create m_CompositeAuthenticator from compositeAuthenticatorSettings
    m_CompositeAuthenticator = new CompositeAuthenticator(compositeAuthenticatorSettings);
    
    // Injecting the CompositeAuthenticator as an IServiceAuthorizer to build the ServiceHttpClient
    var serviceHttpClient = new ServiceHttpClient(httpClient, m_CompositeAuthenticator, playerSettings);
    
    // Injecting the serviceHttpClient to build an authorized IAssetRepository to retrieve IAsset, IDataset, ... from Unity Cloud.
    m_AssetRepository = AssetRepositoryFactory.Create(serviceHttpClient, serviceHostResolver);
    
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)