2 Ways of Authentication in PlayFab – Basics of Player Login

Share

In this post, you will learn the different ways to authenticate your players to use PlayFab with Unity. PlayFab allows for multiple authenication methods. There are two methods you can implement.

  • Anonymous authentication: The client of your game authenticates itself in the background to PlayFab with no user interaction. The same player can later get another identity, and PlayFab will treat it as another player.
  • Recoverable authentication: The player provides credentials manually (username and password or supported third-party logins). The player can log in repeatedly in this way and PlayFab recognize it.

As a best practice, you can start by implementing the anonymous method. Then you recommend your player to register with recoverable methods. This is because asking for user credentials right away can frighten away many people who want to use your game.

Let us see how to implement it.

As a prerequisite, you should already have PlayFab configured in the Unity Editor. If you missed this step, check my earlier post.

Anonymous authentication

The simplest way to start is to register and log in with a “CustomID”, this method applies to clients using desktops. For Android and iOS, you should use device-specific methods.

Register with CustomID on desktop clients

You can register and login with one step by adding “CreateAccount = true” to the request:

var request = new LoginWithCustomIDRequest { CustomId = "ID001", CreateAccount = true };
PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);

private void OnLoginSuccess(LoginResult obj) {     
      Debug.Log("Login with CustomID succeeded.");     
}
private void OnLoginFailure(PlayFabError obj) {     
      Debug.Log("Login with CustomID failed.");
}

The two methods are “OnLoginSuccess” and “OnLoginFailure” which will handle the response. The login will fail when you want to login with a non-existing ID. It is a simple method, and you can observe the new player in PlayFab > Build > Players (clieck on the Search button for query the players):

PlayFab generates a unique Player ID, but if you click on it, you can find the CustomID you assigned to your player:

Recoverable authentication

Recoverable authentication can be done in two ways. Either your player registers with a username and password, or use some third-party authentication provider (such as Google, Facebook).

Register a “standard” (PlayFab) user with username and password

Recoverable authentication does not allow auto-registration with the login request. In case the player is not yet existing or registered, we take these two steps. Firstly, you will start a registration request, including all the needed user credentials:

var request = new RegisterPlayFabUserRequest();

request.Email = "[email protected]";        
request.Username = "johndoe";        
request.Password = "passw0rd";     
request.DisplayName = "JohnDoe";   
     
PlayFabClientAPI.RegisterPlayFabUser(request, OnRegisterPlayFabUserSuccess, OnRegisterPlayFabUserError);

private void OnRegisterPlayFabUserSuccess (RegisterPlayFabUserResult obj) {
        Debug.Log("PlayFab user is registered.");
}
private void OnRegisterPlayFabUserError (PlayFabError obj) {
        Debug.Log("PlayFab user registration is failed.");
}

If the registration is successful, you will be able to observe the new player in PlayFab:

Then, your player can log in any time with the username and password:

var request = new LoginWithPlayFabRequest ();
request.Username = "johndoe";
request.Password = "passw0rd";
PlayFabClientAPI.LoginWithPlayFab(request, OnLoginSuccess, OnLoginFailure);

Be aware that the username, the password, and the email are mandatory fields in the registration request. The username and also the email must be unique for each player.

Register with Google or other third Party authentication provider

PlayFab supports multiple additional authentication methods. You can read detailed tutorials on how to implement them under this link.


Share

Leave a Comment

Your email address will not be published.

Scroll to Top