Tuesday 11 April 2017

How to check if the user has already logged in Unity App

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 PlayerPerfs I read some Q&As on storing data on PlayerPerfs, and most of them mentioned that this might be a wrong choice to store on PlayerPerfs, 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 boolean value, 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 PlayerPerfs.

Can anyone help me out here with ideas or code structure?



via Luzan Baral

No comments:

Post a Comment