Unity Authentication sessions
Manage player sessions by handling tokens, sign-in, sign-out, and token refresh during the player lifecycle.
Read time 3 minutesLast updated 15 hours ago
This page describes the overall Unity Authentication flow from signing in, the token lifecycle, signing out, and general session management.
Signing in
When a player successfully logs in to a device for the first time, the Unity Authentication service grants a long-lived session token. Based on this session token it also grants an access token which is valid for one hour. The access token is used to authenticate the player when calling other Unity services. Both tokens are stored on the device using Unity Player Prefs and are associated with a player profile. For more details on player profiles, refer to Profile management.Token expiry and refreshing
To keep the player authorized beyond the one hour validity period of a single access token, the Unity Authentication service automatically attempts to periodically refresh the access token. If the refresh attempts fail before the expiration time, the access token expires and raises theExpired
When the player signs in successfully the
SignedInSignInAnonymouslyAsync()Signing out
CallingAuthenticationService.Instance.SignOutSignedOutusing Unity.Services.Authentication;void RegisterEvents(){ AuthenticationService.Instance.SignedIn += () => { Debug.Log($"The player has successfully signed in"); }; AuthenticationService.Instance.Expired += () => { Debug.Log($"The access token was not refreshed and has expired"); }; AuthenticationService.Instance.SignedOut += () => { Debug.Log($"The player has successfully signed out"); };}void CheckStates(){ // this is true if the access token exists, but it can be expired or refreshing Debug.Log($"Is SignedIn: {AuthenticationService.Instance.IsSignedIn}"); // this is true if the access token exists and is valid/has not expired Debug.Log($"Is Authorized: {AuthenticationService.Instance.IsAuthorized}"); // this is true if the access token exists but has expired Debug.Log($"Is Expired: {AuthenticationService.Instance.IsExpired}"); // this is true if the access token exists but is being refreshed Debug.Log($"Is Refreshing: {AuthenticationService.Instance.IsRefreshing}");}

Clearing session tokens
By default theSignOutSignInAnonymouslyAsync- Set the optional parameter during sign out, for example:
clearCredentials.AuthenticationService.Instance.SignOut(true) - If the player is already signed out, clear the token using .
AuthenticationService.Instance.ClearSessionToken()
PlayerPrefs
To verify if a session token is currently cached for the current profile, use
AuthenticationService.Instance.SessionTokenExistsusing Unity.Services.Authentication;void SimpleSignOut(){ // The session token will remain but the player will not be authenticated AuthenticationService.Instance.SignOut();}void SignOutAndClearSession(){ // The session token will be deleted immediately, allowing for a new anonymous player to be created AuthenticationService.Instance.SignOut(true);}void SignOutThenClearSession(){ AuthenticationService.Instance.SignOut(); // Do something else... // Now clear the session token to allow a new anonymous player to be created AuthenticationService.Instance.ClearSessionToken();}
Sign in a cached player
A cached player is a player who has their session token persisted on the device. Every time a sign in is successful, the SDK caches a session token. A cached player exists if these conditions are fulfilled:- The player previously signed in with Unity Authentication via anonymous login or platform login.
- The cached session token hasn't been deleted.
AuthenticationService.Instance.SessionTokenExistsSignInAnonymouslyAsync()using Unity.Services.Authentication;async Task SignInCachedUserAsync(){ // Check if a cached player already exists by checking if the session token exists if (!AuthenticationService.Instance.SessionTokenExists) { // if not, then do nothing return; } // Sign in Anonymously // This call will sign in the cached player. try { await AuthenticationService.Instance.SignInAnonymouslyAsync(); Debug.Log("Sign in anonymously succeeded!"); // Shows how to get the playerID Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}