Username/Password

Minimum SDK version: 2.7.2

See the following scenarios in setting up authentication for players in your game with Username/Password:

  • Set up Username/Password.
  • Sign in a returning user or create a new user.
  • Update a user from an anonymous sign-in to a platform sign-in through a Username/Password count.

Note: You first need to initialize the SDK.

Set up Username/Password

  1. Configure your ID provider to be Username/Password for Unity Authentication:
    1. In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication.
    2. Set ID Providers to Username/Password, then select Add.
    3. Select Save.

Sign up or sign in a returning player

Use the SignUpWithUsernamePasswordAsync method to create a new player using Username/Password credentials.

Note 1: The username is case insensitive; it requires a minimum of 3 and a maximum of 20 characters and only supports letters, numbers and symbols like ., -, @ or _. Note 2: The password is case sensitive; it requires a minimum of 8 and a maximum of 30 characters and at least 1 lowercase letter, 1 uppercase letter, 1 number, and 1 symbol.

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);
    }
}

Update a player from anonymous to a Username/Password

After you’ve set up anonymous authentication, if the player wants to upgrade from being anonymous to creating a Username/Password account and sign in, prompt the player to enter their credentials. Call the AddUsernamePasswordAsync API to create a new Username/Password account and add it to the player.

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. Create a new account and add it to the cached player's account with AddUsernamePasswordAsync.

Note: Username/Password accounts cannot be removed after being added to an account.

For more information about cached players, refer to Sign In a Cached User.

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 user must be signed in and you must request the user to enter their current password and new password, then call UpdatePasswordAsync. UpdatePasswordAsync keeps the user's authentication status, but all other signed in devices asks the user to sign back in.

Note: A successful call UpdatePasswordAsync signs the user out from all devices on which the user was cached.

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);
    }
}