question

a0162hm avatar image
a0162hm asked

Login without having to input a username everytime

Hey, i currently have a login system which works fine, you also need to enter a username but lets say im on a different device and i want to log into that account i need to input the username again, if i leave it blank then my username will also be blank. How can i save that username on their account?

the login button logs you in, if the account doesnt exist it will create on for you

Authentication
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Made Wang avatar image
Made Wang answered

You can log in on any device with your email and password, and your code already implements this feature.

Regarding the username, if it is the username in GameManager->Players->[player]->Master Player Account, then it can only be written when calling RegisterPlayFabUser or AddUsernamePassword, and it cannot be updated. From your code, it is not possible for this username to be empty. You can check in GameManager or use the code I provided above to output username.

From your code, I guess you are storing username in PlayerPrefs and getting username from it when you log in, then to clarify, PlayerPrefs is a method in Unity to store some data locally, it cannot be transferred across devices. This is why your username is empty on different devices.

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

a0162hm avatar image a0162hm commented ·

in my onloginfailure function im automatically registering the user including the username they filled in, i checked the master player account and the playfab username is [no username], the playerprefs thing is just for the auto login btw, do i need anything else to save the username or is this it?

    private void OnLoginFailure(PlayFabError error)
    {
        var registerRequest = new RegisterPlayFabUserRequest { Email = userEmail, Password = userPassword, Username = username };
        PlayFabClientAPI.RegisterPlayFabUser(registerRequest, OnRegisterSuccess, OnRegisterFailure);
    }

0 Likes 0 ·
a0162hm avatar image a0162hm a0162hm commented ·

usernames are finally appearing under "master player account" however i still need to login using my username password and email, i was thinking if i login with my email and password it automatically also gets the username like how most games work

i use pun 2 for the lobby system and stuff and when you join a room i do PhotonNetwork.NickName = username, and the username var being the username input, so everytime i login using my email and password (without username) im basically settng my nickname to nothing, is there a way i can directly get playfab username?

0 Likes 0 ·
Made Wang avatar image Made Wang a0162hm commented ·

You can get the username directly when you log in.

PlayFabClientAPI.LoginWithEmailAddress(new LoginWithEmailAddressRequest
{
    Email = "",
    Password = "",
    InfoRequestParameters=new GetPlayerCombinedInfoRequestParams
    {
        GetUserAccountInfo=true
    }
},
(result) =>
{
    string username = result.InfoResultPayload.AccountInfo.Username;
    Debug.Log(username);
},
(error) =>
{
    Debug.LogError(error.GenerateErrorReport());
});

Or call GetAccountInfo to get username.

PlayFabClientAPI.GetAccountInfo(new GetAccountInfoRequest(),
(result) =>
{
    string username = result.AccountInfo.Username;
    Debug.Log(username);
},
(error) =>
{
    Debug.LogError(error.GenerateErrorReport());
});

In addition, it should be noted that Username is sensitive information for the account. It is not recommended to publish it to other players. It is recommended to use DisplayName instead.

1 Like 1 ·
Show more comments
Made Wang avatar image
Made Wang answered

Do you need silent login? You can link the device ID with the account, and then call the API of the corresponding device to achieve silent login, you can refer to Account linking quickstart - PlayFab | Microsoft Docs. And you can link multiple devices.

Also, what do you mean by saving the username in their account? The username should have been written to the profile when registering.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

a0162hm avatar image
a0162hm answered

@Made Wang

does silent login mean that it will auto login after first time registration? if yes, im already saving the email, password and username using playerprefs (which is probably not ideal)


my goal here is to login with an email and password, the user can also input their username. and lets say the user has a second device, i want him to be able to login with that email and password and still retain their username.

the problem that im currently facing is that when the player logs in he has 3 input fields to fill in, email, password, username. if he doesnt fill in his username then it will just be saved as nothing, its almost like the player is registering every single time rather than logging in, ill show some of the code

    public void OnClickLogin()
    {
        var request = new LoginWithEmailAddressRequest { Email = userEmail, Password = userPass };
        PlayFabClientAPI.LoginWithEmailAddress(request, OnLoginSuccess, OnLoginFailure);
    }

    void OnLoginSuccess(LoginResult result)
    {
        PlayerPrefs.SetString("EMAIL", userEmail);
        PlayerPrefs.SetString("PASSWORD", userPass);
        PhotonNetwork.NickName = username2;
    }

    void OnLoginFailure(PlayFabError error)
    {
        var registerRequest = new RegisterPlayFabUserRequest { Email = userEmail, Password = userPass, Username = username2 };
        PlayFabClientAPI.RegisterPlayFabUser(registerRequest, OnRegisterSuccess, OnRegisterFailure);
    }

    void OnRegisterSuccess(RegisterPlayFabUserResult result)
    {
        PlayerPrefs.SetString("EMAIL", userEmail);
        PlayerPrefs.SetString("PASSWORD", userPass);
        PlayerPrefs.SetString("USERNAME", username2);
    }

    public void SubmitNameButton()
    {
        var request = new UpdateUserTitleDisplayNameRequest
        {
            DisplayName = nameInput.text,
        };
        PlayFabClientAPI.UpdateUserTitleDisplayName(request, OnDisplayNameUpdate, OnError);
    }


    public void OnDisplayNameUpdate(UpdateUserTitleDisplayNameResult result)
    {
        username.text = result.DisplayName;
    }


4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Made Wang avatar image Made Wang commented ·

Silent login means that when you link a device ID for a PlayFab account, then you can use the login method corresponding to different devices to log in without having to enter your email and password every time.

The email and password can be used to log in on different devices and your code works fine in my tests. You mentioned that if the player does not fill in the username when logging in (in fact, LoginWithEmailAddress does not require a username) then the registration function will be executed, but your registration method will fail if the username is empty, refer to the description of RequireBothUsernameAndEmail in Authentication - Register PlayFab User - REST API (PlayFab Client) | Microsoft Docs.

You can try adding some Debug.Log() to output error messages, and you can also use breakpoint debugging to check the execution order of the code.

0 Likes 0 ·
a0162hm avatar image a0162hm Made Wang commented ·

what i mean was that when the user does not input a username it saves that username as nothing, even when logging in, so instead of logging in with your email and password (without the username because you already did that when creating the account) it also saves the username again

im new to unity and networking and i really cant figure out how im supposed to fix this, ive tried everything

0 Likes 0 ·
Made Wang avatar image Made Wang a0162hm commented ·

What you want is to save the player's username locally when logging in? You can refer to the code below.

PlayFabClientAPI.LoginWithEmailAddress(new LoginWithEmailAddressRequest
{
    Email = "",
    Password = "",
    InfoRequestParameters=new GetPlayerCombinedInfoRequestParams
    {
        GetUserAccountInfo=true
    }
},
(result) =>
{
    PlayerPrefs.SetString("USERNAME", result.InfoResultPayload.AccountInfo.Username);
},
(error) =>
{
    Debug.LogError(error.GenerateErrorReport());
});
0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.