Play-games-plugin-for-unity: Social.localUser.Authenticate: User Does Not Stay Logged In

Created on 14 Jan 2015  路  9Comments  路  Source: playgameservices/play-games-plugin-for-unity

I've added the plugin to my Unity project and everything seems to work fine except for keeping the user logged into Google Play Games Services.

I use the following code to activate the plugin:

void Awake()
{
PlayGamesPlatform.Activate();

}

I use the following code to log in/out:

if (!Social.localUser.authenticated) {
Social.localUser.Authenticate((bool success) => {
if (success) {
} else {
}
});
} else {
((GooglePlayGames.PlayGamesPlatform) Social.Active).SignOut();
}

The problem I'm having is -- if I go to another scene or quit the game and come back, the user gets logged out. The expected result is the user should stay logged in. Is anyone else having this issue? Please let me know if I'm missing something.

Most helpful comment

Best to open a new issue - so it does not get lost in the shuffle :).

I think you just need to change

        if (!PlayGamesPlatform.Instance.localUser.authenticated) {
            LogIn.enabled = true;
        }

to

        if (!PlayGamesPlatform.Instance.localUser.authenticated) {
            LogIn.enabled = true;
        } else {
            LogIn.enabled = false;
           userName.text = PlayGamesPlatform.Instance.localUser.userName;
       }

All 9 comments

My account has already been added to the list of testers in the dashboard.

I have the same issue. After entering/loading another scene user gets logged in again.
Tried both Social.localUser.authenticated and PlayGamesPlatform.Instance.IsAuthenticated().
Would appreciate some advice.

I tried both ways too and still ran into issues.

I didn't want to automatically attempt to login the user when the game starts because that would anger a lot of users. So what I did was -- if the user clicks the Google+ login button and successfully authenticates, I'd save a value to user PlayerPrefs indicating that we should automatically try to log them in when they start the game and when a new scene is loaded:

// Check for Auto Login
        if (PlayerPrefs.GetInt("AutoLogin", 0) == 1) {
            debugLabel.text = "Auto login enabled";

            if (!Social.localUser.authenticated) {
                debugLabel.text = "User not logged in yet";
                Social.localUser.Authenticate((bool success) => {
                    if (success) {
                        debugLabel.text = "Logged in successfully";
                    } else {
                        debugLabel.text = "Log in failed miserably";
                    }
                });
            }
        } else {
            debugLabel.text = "Auto login is not enabled";
        }

This seems to work around the issue for me. After the user logs in for the first time, when I automatically log them in the second time it happens instantaneously and the Google+ login box doesn't show up again.

Same Problem goes with me on Android. When user logged in once and quit the application and come back again, user seems to be logged out. Any Help?

edit: I am using unity4.6.1(Latest one) and gps plugin 0.9.11

Hey, gang. I believe there's a simpler solution -- you should try silent authentication first.

        if (! PlayGamesPlatform.Instance.localUser.authenticated) {
            PlayGamesPlatform.Instance.Authenticate ((bool success) => {
                if (success) {
                    /// Signed in! Hooray!
                } else {
                    /// Not signed in. We'll want to show a sign in button
                }
            }, true);   /// <--- That "true" is very important!
        } else {
            Debug.Log("We're already signed in");
        }

See that true at the end of the Authenticate call? That attempts to sign the user in silently (i.e. without any dialog boxes). This will succeed if the user has signed in previously, and you're not asking for any new scopes. So call this at the start of your app, and it will re-sign in your user without their needing to do anything.

If this fails, it's probably because you have a new user who isn't signed in. At that point, you can show a sign-in button and call authenticate the way you normally do when the user clicks it.

@ToddKerpelman Thank you very much for your reply. That is exactly what I needed. Everything is working beautifully.

@ToddKerpelman i know it's closed but i have one problem with the login

When i start the game i can log in. All is fine. The login button hides when i'm logged in. I can see the username. But! When i change the scene and then when i go back to the main menu the login button is there.

This is my script:

`void Start(){

        LogInfo = LogInfo.GetComponent<Text> ();
        LogIn = LogIn.GetComponent<Image> ();

        userName = userName.GetComponent<Text> ();
        Leaderboard = Leaderboard.GetComponent<Image> ();

        userName.enabled = false;

        if (!PlayGamesPlatform.Instance.localUser.authenticated) {
            LogIn.enabled = true;
        }

        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()

            .Build();

        PlayGamesPlatform.InitializeInstance(config);
        // recommended for debugging:
        PlayGamesPlatform.DebugLogEnabled = true;
        // Activate the Google Play Games platform



        PlayGamesPlatform.Activate();
        if (!PlayGamesPlatform.Instance.localUser.authenticated) {
            PlayGamesPlatform.Instance.Authenticate ((bool success) => {
                if (success) {
                    Debug.Log ("You've successfully logged in");
                    LogInfo.text = "Success";
                    userName.enabled = true;
                    userName.text = "Welcome " + PlayGamesPlatform.Instance.localUser.userName;
                    LogIn.enabled = false;
                } else {
                    Debug.Log ("Login failed for some reason");
                    LogInfo.text = "Failed";
                }
            },true);
        }
    }

    public void LeaderBoard(){
        if (PlayGamesPlatform.Instance.localUser.authenticated) {

            ((PlayGamesPlatform)Social.Active).ShowLeaderboardUI(leaderboard_high_score);


        }
    }


    public void LogIN(){
    PlayGamesPlatform.Instance.Authenticate ((bool success) => {
        if (success) {
            Debug.Log ("You've successfully logged in");
            LogInfo.text = "Success";
            userName.text = PlayGamesPlatform.Instance.localUser.userName;
            LogIn.enabled = false;
        } else {
            Debug.Log ("Login failed for some reason");
            LogInfo.text = "Failed";
        }
    });
    }`

Best to open a new issue - so it does not get lost in the shuffle :).

I think you just need to change

        if (!PlayGamesPlatform.Instance.localUser.authenticated) {
            LogIn.enabled = true;
        }

to

        if (!PlayGamesPlatform.Instance.localUser.authenticated) {
            LogIn.enabled = true;
        } else {
            LogIn.enabled = false;
           userName.text = PlayGamesPlatform.Instance.localUser.userName;
       }

@claywilkinson i try this and tell you in 3 minutes the result.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

temresen picture temresen  路  4Comments

brentbartlett picture brentbartlett  路  5Comments

RafikTSG picture RafikTSG  路  3Comments

Only4Gamers picture Only4Gamers  路  4Comments

YogendraChauhan picture YogendraChauhan  路  4Comments