Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

What is the proper way to check if the user has already logged in into Unity App

Discussion in 'Scripting' started by luzanb, Apr 12, 2017.

  1. luzanb

    luzanb

    Joined:
    Feb 25, 2017
    Posts:
    18
    I am using Facebook Login and Native Email Registration/Login for a new App in Unity. Checking the login state for Facebook was easier with the SDK, using FB.IsLoggedIn. But, here I am trying to check if the user has logged in into the app already using the Native Email Login, and if a user has already logged in then s/he don't have to login every time.

    I am able to login to the app, back-end for the app is a Node Server, from where I get user and token as return when one logs in successfully, this is done using passport-local. This token is used every time as authentication token to play game and to perform other options, but to use all functions a user has to login. I couldn't figure out how to handle these states in Unity.

    {
    "user": "luzan@email.com,
    "token": "sometoken"
    }
    Using PlayerPrefs I read some Q&As on storing data on PlayerPrefs, and most of them mentioned that this might be a wrong choice to store on PlayerPrefs, as this is easily modifiable.

    My thoughts on this, I was also thinking of handling it from the Node Server itself, as this is my current mongodb document for account,

    {
    "_id" : ObjectId("58e73931c2369f1bd8c89ebd"),
    "salt" : "saltkey",
    "hash" : "somehashkey",
    "username" : "luzan@email.com",
    "__v" : 0
    }
    I was thinking of adding another field (say) _isLoggedIn to the MongoDB schema with the booleanvalue, that is set to true if the player has loggedin once and has not log out from the app. And check that every time to see if player has already logged in or not. But again, to access that document through API, I'll be needing the same token to be passed with API. And I am still confused if that token should be saved on PlayerPrefs.

    Can anyone help me out here with ideas or code structure?
     
    Last edited: Apr 12, 2017
  2. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    You got Answer??
     
  3. rangesstudio

    rangesstudio

    Joined:
    Nov 23, 2021
    Posts:
    6
    Old question, but still a good solution would be using a DontDestroyOnLoad object and checking if it exists before authentication. Then if it exists we know that the player is signed in, and if not we now to sign in the player.
     
  4. rangesstudio

    rangesstudio

    Joined:
    Nov 23, 2021
    Posts:
    6
    upload_2023-7-14_21-30-34.png

    My code:

    if (GameObject.Find("AuthenticatedGameObject") == null)
    {
    await UnityServices.InitializeAsync();

    AuthenticationService.Instance.SignedIn += () =>
    {
    Debug.Log("Signed in " + AuthenticationService.Instance.PlayerId);
    };

    await AuthenticationService.Instance.SignInAnonymouslyAsync();

    GameObject AuthenticatedGameObject = new GameObject("AuthenticatedGameObject");
    DontDestroyOnLoad(AuthenticatedGameObject);
    }