question

anudeep avatar image
anudeep asked

Remember Me Functionality isn't working as desired.

So I have followed what happens in PlayfabSamples, the PlayFabAuthService script specifically. However I am not able to remember the account for reasons I can't figure out. Would love some insight on what the issue is,

Here is my code, which handles all the functions related PlayFab called PlayFabManager

public bool RememberMe;

protected override void Awake()
    {
        base.Awake();
        ClearPlayerPrefs = true;
        if (ClearPlayerPrefs)
        {
            
            _AuthService.ClearRememberMe();
            _AuthService.AuthType = Authtypes.None;
        }
        _AuthService.RememberMe = RememberMe;
    }

This RememberMe is toggled in another script, the point in is both PlayFabManager.Instance.RememberMe and _AuthService.Remember me become true/false on toggle.

public bool RememberMe
    {
        get
        {
            return PlayerPrefs.GetInt(_LoginRememberKey, 0) == 0 ? false : true;
        }
        set
        {
            Debug.Log("Remembering the value");
            PlayerPrefs.SetInt(_LoginRememberKey, value ? 1 : 0);
            Debug.Log(PlayerPrefs.GetInt(_LoginRememberKey));
        }
    }

    /// <summary>
    /// Remember the type of authenticate for the user
    /// </summary>
    public Authtypes AuthType
    {
        get
        {
            return (Authtypes)PlayerPrefs.GetInt(_PlayFabAuthTypeKey, 0);
        }
        set
        {
            
            PlayerPrefs.SetInt(_PlayFabAuthTypeKey, (int)value);
        }
    }

    /// <summary>
    /// Generated Remember Me ID
    /// Pass Null for a value to have one auto-generated.
    /// </summary>
    private string RememberMeId
    {
        get
        {
            return PlayerPrefs.GetString(_PlayFabRememberMeIdKey, "");
        }
        set
        {
            var guid = string.IsNullOrEmpty(value) ? Guid.NewGuid().ToString() : value;
           /// What is happening in the line above? 
            PlayerPrefs.SetString(_PlayFabRememberMeIdKey, guid);
        }
    }

public void ClearRememberMe()
    {
        PlayerPrefs.DeleteKey(_LoginRememberKey);
        PlayerPrefs.DeleteKey(_PlayFabRememberMeIdKey);
        PlayerPrefs.DeleteKey(_PlayFabAuthTypeKey);
    }

    /// <summary>
    /// Kick off the authentication process by specific authtype.
    /// </summary>
    /// <param name="authType"></param>
    public void Authenticate(Authtypes authType)
    {
        AuthType = authType;
        Authenticate();
    }

private void AuthenticateEmailPassword()
    {
        //Check if the users has opted to be remembered.
        if (RememberMe && !string.IsNullOrEmpty(RememberMeId))
        {
            //If the user is being remembered, then log them in with a customid that was 
            //generated by the RememberMeId property
            PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
            {
                TitleId = PlayFabSettings.TitleId,
                CustomId = RememberMeId,
                CreateAccount = true,
                InfoRequestParameters = InfoRequestParams
            }, (result) =>
            {
                //Store identity and session
                _playFabId = result.PlayFabId;
                _sessionTicket = result.SessionTicket;

                if (OnLoginSuccess != null)
                {
                    //report login result back to subscriber
                    OnLoginSuccess.Invoke(result);
                }
            }, (error) =>
            {
                if (OnPlayFabError != null)
                {
                    //report error back to subscriber
                    OnPlayFabError.Invoke(error);
                }
            });
            return;
        }

        //a good catch: If username & password is empty, then do not continue, and Call back to Authentication UI Display 
        if (!RememberMe && string.IsNullOrEmpty(Email) && string.IsNullOrEmpty(Password))
        {
            OnDisplayAuthentication.Invoke();
            return;
        }


        //We have not opted for remember me in a previous session, so now we have to login the user with email & password.
        PlayFabClientAPI.LoginWithEmailAddress(new LoginWithEmailAddressRequest()
        {
            TitleId = PlayFabSettings.TitleId,
            Email = Email,
            Password = Password,
            InfoRequestParameters = InfoRequestParams
        }, (result) =>
        {
            //store identity and session
            _playFabId = result.PlayFabId;
            _sessionTicket = result.SessionTicket;
            

            //Note: At this point, they already have an account with PlayFab using a Username (email) & Password
            //If RememberMe is checked, then generate a new Guid for Login with CustomId.
            if (RememberMe)
            {
                Debug.Log("R E M E M B E R I N G");

//It debugs the above line in the unity console, however nothing gets remembered.
                RememberMeId = Guid.NewGuid().ToString();
                AuthType = Authtypes.EmailAndPassword;
                //Fire and forget, but link a custom ID to this PlayFab Account.
                PlayFabClientAPI.LinkCustomID(new LinkCustomIDRequest()
                {
                    CustomId = RememberMeId,
                    ForceLink = ForceLink
                }, null, null);
            }

            if (OnLoginSuccess != null)
            {
                //report login result back to subscriber
                OnLoginSuccess.Invoke(result);
            }
        }, (error) =>
        {
            if (OnPlayFabError != null)
            {
                //Report error back to subscriber
                OnPlayFabError.Invoke(error);
            }
        });
    }

Although It iterates through it all, don't know what is wrong that it doesn't work through.

While checking for the values, everything pretty much gave strings.

"PlayFabLoginRemember" for (_LoginRememberKey), "PlayFabAuthType" for (_PlayFabAuthTypeKey), "PlayFabIdPassGuid" for (_PlayFabRememberMeIdKey) and only RememberMeId returns some sort of value.

What is going wrong here?

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.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

>> Although It iterates through it all, don't know what is wrong that it doesn't work through.

I think the issue may be related to the Awake function. The “RememberMe” is relay on the info store in the PlayerPrefs, if you clear the PlayerPrefs every time you start the game, then the “RememberMe” cannot work.

>> While checking for the values, everything pretty much gave strings. "PlayFabLoginRemember" for (_LoginRememberKey), "PlayFabAuthType" for (_PlayFabAuthTypeKey), "PlayFabIdPassGuid" for (_PlayFabRememberMeIdKey) and only RememberMeId returns some sort of value.

“_LoginRememberKey” “_PlayFabAuthTypeKey” “_PlayFabRememberMeIdKey” are defined as const strings, if you want to check if the player has been remembered, you should check the value of “RememberMeId”, “RememberMe” and “AuthType”. Since you mention that the RememberMeId returns some value, the player should be remembered.

10 |1200

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

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.