Documentation

Support

Authentication

Username and Password

Provide a username and password option to enable players to authenticate using custom credentials in your game.
Read time 3 minutesLast updated 15 hours ago

Minimum SDK version: 2.7.2
Username & Password is an identity provider which is provided natively by the Unity Authentication service and the SDK. It provides support for the following scenarios:
  • Set up the Username & Password provider.
  • Sign in a returning player or sign up a new player.
  • Link an existing player to Username & Password.
  • Change a player's password (assuming the old password is known).

Set up the Username & Password provider

  1. Initialize the Unity Authentication SDK.
  2. Add a configuration for the Username & Password provider in Unity Authentication using one of the following options:
    • In the Unity Editor:
      1. Go to Services > Authentication > Configure.
      2. Set the ID Providers dropdown to Username and Password, then select Add.
      3. Select Save.
    • In the Unity Dashboard:
      1. Navigate to the Player Authentication service page.
      2. Open the Add Identity Provider dropdown and select Username & Password.
When a player has signed up with Username & Password, it will display as a linked identity in the Player Management dashboard. The details of the username and password are not displayed.

Sign up or sign in a returning player

Use the
SignUpWithUsernamePasswordAsync
method to create a new player with Username & Password credentials.
async Task SignUpWithUsernamePasswordAsync(string username, string password){ try { await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync(username, password); Debug.Log("SignUp is successful."); } 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); }}
Use the
SignInWithUsernamePasswordAsync
method to sign in an existing player using the Username & Password credentials:
async Task SignInWithUsernamePasswordAsync(string username, string password){ try { await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(username, password); Debug.Log("SignIn is successful."); } 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); }}

Link an existing player to Username & Password

If a player is already signed in with another provider, either anonymously or using a platform provider, then they can be linked to a Username & Password. This allows them to sign in using their username and password in addition to any other providers. To do so, prompt the player to enter their desired username and password and call the
AddUsernamePasswordAsync
API to link the player with the provided credentials.
If a cached player exists on the SDK, create a new Username & Password and add it to the cached player.
  1. Sign in the cached player's account using
    SignInAnonymouslyAsync
    .
  2. Add it to the cached player's account with
    AddUsernamePasswordAsync
    .
For more information about cached players, refer to Sign in a cached player.
async Task AddUsernamePasswordAsync(string username, string password){ try { await AuthenticationService.Instance.AddUsernamePasswordAsync(username, password); Debug.Log("Username and password added."); } 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); }}

Change a player's password

Give players the option to change the password on their Username & Password accounts. The player must be signed in and you must request the player to enter their current password and new password, then call
UpdatePasswordAsync
.
UpdatePasswordAsync
keeps the player's authentication status, but all other signed in devices asks the player to sign back in.
This method doesn't support password recovery. If the current password isn't known, then password recovery must be implemented as a custom server-authoratative flow using the relevant admin API endpoint. This requires separate admin authentication, for example, using Service Accounts, and must not be called from the game client directly.
async Task UpdatePasswordAsync(string currentPassword, string newPassword){ try { await AuthenticationService.Instance.UpdatePasswordAsync(currentPassword, newPassword); Debug.Log("Password updated."); } 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); }}