简体   繁体   English

如何检查用户是否已登录Unity App

[英]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. 我正在使用Facebook登录和本机电子邮件注册/登录Unity中的新应用程序。 Checking the login state for Facebook was easier with the SDK, using FB.IsLoggedIn . 使用FB.IsLoggedIn ,SDK可以更轻松地检查Facebook的登录状态。 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 . 我能够登录到该应用程序,该应用程序的后端是一个节点服务器,当我成功登录时,我从那里获得usertoken作为返回,这是使用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. 我无法弄清楚如何在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. 使用PlayerPrefs我读了一些关于在PlayerPrefs上存储数据的PlayerPrefs ,并且大多数人都提到这可能是存储在PlayerPrefs上的错误选择,因为这很容易修改。

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, 我对此的想法 ,我也在考虑从Node Server本身处理它,因为这是我目前用于帐户的mongodb文档,

{
    "_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. 我正在考虑使用boolean值将另一个字段(比方说) _isLoggedIn到MongoDB模式中,如果播放器已登录一次但未从应用程序注销,则设置为true 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. 但同样,要通过API访问该文档,我将需要使用API​​传递相同的标记。 And I am still confused if that token should be saved on PlayerPrefs . 如果该token应保存在PlayerPrefs上,我仍然感到困惑。

Can anyone help me out here with ideas or code structure? 任何人都可以通过想法或代码结构帮助我吗?

As far as i understand, your problem is to know at a given time if your user is logged in or not if so, 据我所知,您的问题是在给定时间知道您的用户是否已登录,如果是,

According to the documentation Manage Users in Firebase 根据文档管理Firebase中的用户

It's recommended that you get the current user by setting a listener on the Auth object 建议您通过在Auth对象上设置侦听器来获取当前用户

Firebase.Auth.FirebaseAuth auth;
Firebase.Auth.FirebaseUser user;



 // Handle initialization of the necessary firebase modules:
    void InitializeFirebase() {
      Debug.Log("Setting up Firebase Auth");
      auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
      auth.StateChanged += AuthStateChanged;
      AuthStateChanged(this, null);
    }

// Track state changes of the auth object.
    void AuthStateChanged(object sender, System.EventArgs eventArgs) {
        if (auth.CurrentUser != user) {
            bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
            if (!signedIn && user != null) {
              Debug.Log("Signed out " + user.UserId);
            }
            user = auth.CurrentUser;
            if (signedIn) {
              Debug.Log("Signed in " + user.UserId);
            }
        }
    }

    void OnDestroy() {
      auth.StateChanged -= AuthStateChanged;
      auth = null;
    }

and you can get the user profile as follows 您可以按如下方式获取用户配置文件

    Firebase.Auth.FirebaseUser user = auth.CurrentUser;
    if (user != null) {
      string name = user.DisplayName;
      string email = user.Email;
      System.Uri photo_url = user.PhotoUrl;
      // The user's Id, unique to the Firebase project.
      // Do NOT use this value to authenticate with your backend     server, if you
      // have one; use User.TokenAsync() instead.
      string uid = user.UserId;
    }

i don't know if the user profile is preserved when the app is closed or not,So check it out. 我不知道应用程序关闭时是否保留了用户配置文件,请查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM