-
Notifications
You must be signed in to change notification settings - Fork 0
Mobile Authentication
Now we are going to see how to anonymous login and signup out mobile users in order to let users play our game without the need to sign up and when they decide they want to signup we associate their android device and the gamedata to the new account they are going to create.
In the previos chapter we saw the usuar login and we set the next code to check if the playerPrefs had alredy an user, in order to grant acces to the user faster.
if (PlayerPrefs.HasKey("EMAIL")) {
userEmail = PlayerPrefs.GetString("EMAIL"); userPassword = PlayerPrefs.GetString("PASSWORD");
var request = new LoginWithCustomIDRequest { CustomId = "GettingStartedGuide", CreateAccount = true };
PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
}Now to that code we are going to add an else in case the playerPrefs doesn't have an user alredy.
else {
//MobileLogin
playFabMobileAuth.MobileAuthentication();
}This playfabMobileAuth is a new class we created that will handle all the mobile authentication, including the anonymous login. Here is that MobileAuthentication() method of that class:
public void MobileAuthentication() {
#if UNITY_ANDROID
var requestAndroid = new LoginWithAndroidDeviceIDRequest { AndroidDeviceId = ReturnMobileID(), CreateAccount = true };
PlayFabClientAPI.LoginWithAndroidDeviceID(requestAndroid, OnLoginSuccessMobile, OnLoginFailureMobile);
#endif
#if UNITY_IOS
var requestIOS = new LoginWithAndroidDeviceIDRequest { DeviceId = ReturnMobileID(), CreateAccount = true};
PlayFabClientAPI.loginWithIOSDeviceID(requestIOS, OnLoginSuccessMobile, OnLoginFailureMobile)
#endif
}In this method we check if the mobile device is an Android or IOS device. Depending on this we are going to Execute a login with the DeviceID unique to every device. We create that request with that DeviceID and execute the LoginWithAndroidDeviceID, and assign a success and failure callback methods that basically will activate and deactivate gameobjects in our scene to start the game for example.
For linking a new account to this device, we are going to use the function of AddUserNamePassword that playfab provide us, like this:
public void OnClickLinkAccountButton() {
var LinkAccountRequest = new AddUsernamePasswordRequest
{
Email = userEmailMobile,
Password = userPasswordMobile,
Username = usernameMobile
};
PlayFabClientAPI.AddUsernamePassword(LinkAccountRequest, OnLinkAccountSuccess, OnRegisterFailureMobile);
}In case we succesfully link the device to a new account we save this data to the PlayerPrefs.
private void OnLinkAccountSuccess(AddUsernamePasswordResult result) {
Debug.Log("Account Linked Succesfull!");
PlayerPrefs.SetString("EMAIL", userEmailMobile);
PlayerPrefs.SetString("PASSWORD", userPasswordMobile);
UISingleton.instance.loginPanel.SetActive(false);
UISingleton.instance.logoutButton.SetActive(true);
}And thats all we need to now for now about the mobile authentication.
Home
C#
Game Design Patterns
- Command
- Flyweight pattern & Scriptable Objects
- Observer pattern
- State pattern
- Object Pool pattern
- Factory pattern
ECS
PlayFab
- Introduction
- Template Class
- Player Authentication
- Mobile Authentication
- Player Statistics
- Leaderboard
- Player Data
- Friends
Mirror