Devlog #5 - User Authentication with PlayFab


With Playfab, users can register an account, securely storing account details like usernames and email addresses in its database, allowing players to easily log back in and pick up where they left off. This system will also prove invaluable for future features, such as character customisation, as players will be able to create and save their own unique characters. 

Why Playfab?

I chose the Playfab as it's well-supported and easy to set up with Unity. It provides tools for player authentication and leaderboard management. This allows for a more persistant player experience and reduces the need for custom backend development. 

Registering New Users

For new players, the registration function collects the display name, email and password, then sends it to PlayFab: 

PlayfabLogin.cs


Once registered, the OnRegisterSuccess callback confirms account creation. I decided not to require both a username and an email to simplify the testing process for now, though this can be toggled later for more control. 

Successful Register


Logging in Users

To log in a returning player, I created a function that takes the email and password input from the UI and sends it to PlayFab. 

PlayfabLogin.cs


This function authenticates the user by sending the credentials to PlayFab. If successful, OnLoginSuccess is triggered and we gain access to the player's profile.

Handling a Successful Login

Once a user logs in, the game needs to fetch their display name. I created a function to handle this logic cleanly and show a personalised welcome message:

PlayfabLogin.cs


Heres what this functions does:

  • Fetches the users display name from their PlayFab profile.
  • Displays a personalised welcome message in the UI showing their username.
  • Stores the players name and ID in a centralised GameManager, which other scripts use to get the player's username.
  • Transitions the player into the lobby scene. 

Successful login


Syncing PlayFab User Data with Photon

To make sure the player's display name from PlayFab is also used in Photon PUN, I created a system that stores the player's data. This helps keep player identity consistent in any scene. 

Assigning Player Data

GameManager.cs

This function stores the player's display name and PlayFab ID in the GameManager. The GameManager is a singleton used to persist data across scenes. This is useful for identifying players later e.g. leaderboards. 

Setting the Photon Nickname

ConnectToServer.cs


When the player has connected to the master server, I use the stored PlayFab name to set the player's Photon nickname. This ensures that when the player joins a room or lobby, their name is shown correctly to other players.

Reset Password

To ensure players don't get locked out of their accounts, I added a password recovery system using PlayFab's account recovery email feature. If a player forgets their password, they can enter their email and receive a reset link. 

PlayfabLogin.cs

This function sends a recovery email to the provided address.  The TitleId is a unique identifier for the game in PlayFab - it is crucial for verifying the correct project. Once the request is sent, OnResetSuccess confirms it, while OnError handles any issues (like invalid emails).

Reset Password UI


Guest Login

To make the game more accessible, I added a "Play as Guest" feature. This allows players to jump into the game without needing to register or enter credentials. It is a quick way for players to try the game and is especially useful for first-time users. 

PlayfabLogin.cs

This function generates a unique GUID as a CustomId, then logs in using PlayFab's LoginWithCustomID method. If the ID doesn't exist, a new guest account is created automatically.

Setting the Guest Display Name

After a successful login, the game assigns a custom guest name using a portion of the GUID. This display name is updated on PlayFab and shown in the UI. 

PlayfabLogin.cs

Once the display name is set, the guest player is transitioned into the lobby scene. The guest player will now have it's own unique username. 

Play As Guest Success


Milestones Met

✅ Register User

Players can now create accounts by entering their email, password and choosing a unique username, adding some personalisation to the game. This system is great for persistent user data and future features such as character customisation and leaderboards. 

✅ Login User

Once registered, players can log back in with their credentials to continue where they left off.  This will be handy when character customisation has been created, as it means players can save their characters. 

✅ Forgot Password

If a player forgets their password, they can recover access by entering their email address. A password recovery email is sent via PlayFab, ensuring users don't lose access to their progress or content. 

✅ Play As Guest

Players can now jump into the game instantly with the "Play As Guest" button. Suitable for new players or players that don't want to create an account. 

What's Next?...

With user authentication now in place via PlayFab, the foundation for persistent player data and future features like saving character customisation data is set. 

Looking ahead, the next milestone will be upgrading the lobby system, I want to transition it from a 2D interface to a 3D space. This change will allow players to see the characters they customise. 

References

[1] Microsoft Azure PlayFab.  Data Management. Available at:https://playfab.com/analytics/#data-management.

[2] Microsoft. Quickstart: Set and get player data. Available at: https://learn.microsoft.com/en-us/gaming/playfab/player-progression/player-data/quickstart.

[3] Microsoft. Game Manager reference. Available at: https://learn.microsoft.com/en-us/gaming/playfab/live-service-management/gamemanager/reference.

Leave a comment

Log in with itch.io to leave a comment.