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
- Initialize the Unity Authentication SDK.
- Add a configuration for the Username & Password provider in Unity Authentication using one of the following options:
- In the Unity Editor:
- 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:
Sign up or sign in a returning player
Use theSignUpWithUsernamePasswordAsyncUse theasync 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); }}
SignInWithUsernamePasswordAsyncasync 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 theAddUsernamePasswordAsync- Sign in the cached player's account using .
SignInAnonymouslyAsync - 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 callUpdatePasswordAsyncUpdatePasswordAsyncasync 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); }}