Username & Password
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).
The username used by this provider is completely separate from the player name.
Password recovery must be implemented as a custom server-authoratative flow using the relevant admin API endpoint. This requires separate admin authentication, e.g. using Service Accounts, and must not be called from the game client directly.
Set up the Username & Password provider
- Ensure the Unity Authentication SDK is initialized
- Add a configuration for the Username & Password provider in Unity Authentication using one of the two following options:
- In the Unity Editor
- In the Unity Editor menu, go to Services > Authentication > Configure.
- Set the ID Providers dropdown to Username and Password, then select Add.
- Select Save.
- In the Unity dashboard
- Navigate to the Player Authentication service page
- Open the Add Identity Provider dropdown and select Username & Password
- In the Unity Editor
After 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.
Note: The username is case insensitive; it requires a minimum of 3 and a maximum of 20 characters and only supports letters A-Z and a-z, numbers and the symbols ., -, @ and _.
Note: 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);
}
}
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 will allow 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.
- Sign in the cached player's account using
SignInAnonymouslyAsync
. - Add it to the cached player's account with
AddUsernamePasswordAsync
.
Note: Username & Password cannot be removed after being linked to a player.
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 does not support password recovery. If the current password is not known, then password recovery must be implemented as a custom server-authoratative flow using the relevant admin API endpoint. This requires separate admin authentication, e.g. using Service Accounts, and must not be called from the game client directly.
Note: A successful call UpdatePasswordAsync
signs the player out from all devices on which the player 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);
}
}