Firebase:如何保持Android用户登录状态? [英] Firebase: How to keep an Android user logged in?

查看:181
本文介绍了Firebase:如何保持Android用户登录状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase SimpleLogin启用电子邮件/密码身份验证.创建用户和随后的登录都可以正常工作.但是,每当我离开应用程序时(即使只有几秒钟),用户也永远不会登录我的返回列表,即...

I'm using Firebase SimpleLogin to enable Email / Password authentication. Creation of users and subsequent login is all working fine. However, whenever I leave the app (even if only for a few seconds) the user is never logged in on my return i.e...

authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler())...

总是返回一个空用户.

我没有通过API注销用户.另外,我还在Firebase控制台中将用户登录的天数设置为21.

I am not logging out the user via the API. Also I have set the number of days the user is logged in to 21 in the Firebase console.

我已经在JS文档中看到了我记得的参数,但是我看不到任何适用于Android/Java的参数.

I have seen mention of a remember-me param in the JS docs, but I can't see any equivalent for Android / Java.

想知道我是否遗漏了文档中的任何内容,或者Android是否无法做到这一点?

Wondering if I'm missing anything in the docs or if it's not possible for Android?

感谢您的帮助,

尼尔.

添加了代码示例.

用户创建....

public void registerUserForChat(final MyApplication application, String email, String password) {
    Firebase ref = new Firebase(FIREBASE_URL);
    SimpleLogin authClient = new SimpleLogin(ref);
    authClient.createUser(email, password, new SimpleLoginAuthenticatedHandler() {
        @Override
        public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
            if(error != null) {
                Log.e(TAG, "Error attempting to create new Firebase User: " + error);
            }
            else {
                Log.d(TAG, "User successfully registered for Firebase");
                application.setLoggedIntoChat(true);
            }
        }
    });
}

用户登录....

public void loginUserForChat(final MyApplication application,  String email, String password) {
    Log.d(TAG, "Attempting to login Firebase user...");
    Firebase ref = new Firebase(FirebaseService.FIREBASE_URL);
    final SimpleLogin authClient = new SimpleLogin(ref);
    authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler() {
        @Override
        public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
            if (error != null) {
                Log.d(TAG, "error performing check: " + error);
            } else if (user == null) {
                Log.d(TAG, "no user logged in. Will login...");
                authClient.loginWithEmail(email, password, new SimpleLoginAuthenticatedHandler() {
                    @Override
                    public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
                        if(error != null) {
                            if(com.firebase.simplelogin.enums.Error.UserDoesNotExist == error) {
                                Log.e(TAG, "UserDoesNotExist!");
                            } else {
                                Log.e(TAG, "Error attempting to login Firebase User: " + error);
                            }
                        }
                        else {
                            Log.d(TAG, "User successfully logged into Firebase");
                            application.setLoggedIntoChat(true);
                        }
                    }
                });
            } else {
                Log.d(TAG, "user is logged in");
            }
        }
    });
}

因此,loginUserForChat方法首先检查是否有登录用户,如果没有,则执行登录.请注意,每次启动该应用程序时,我看到的日志都是....

So loginUserForChat method first checks to see if there is a logged in user and, if not, performs the login. Note that every time I start the app, the logging I see is....

  1. 尝试登录Firebase用户...
  2. 没有用户登录.将登录...
  3. 用户成功登录Firebase

如果我退出该应用程序,即使是几秒钟,然后返回-我也会看到相同的日志记录.

If I exit the app, even for a few seconds, and return - I see the same logging.

我注意到的一件事是,对 checkAuthStatus 的调用没有任何用户凭据-我假设它只是检查本地登录用户的 any 吗?

One thing I noticed is that the call to checkAuthStatus does not take any user credentials - I assume it just checks for any locally logged in user?

非常感谢.

推荐答案

[Firebase的工程师]为了透明地处理Firebase Simple Login Java客户端中的持久性会话,您需要使用接受Android的两个参数的构造函数上下文,即每次实例化简单登录Java客户端时为SimpleLogin(com.firebase.client.Firebase ref, android.content.Context context).

[Engineer at Firebase] In order to transparently handle persistent sessions in the Firebase Simple Login Java client, you need to use the two-argument constructor which accepts an Android context, i.e. SimpleLogin(com.firebase.client.Firebase ref, android.content.Context context) every time you instantiate the Simple Login Java client.

请参见 https://www.firebase. com/docs/java-simple-login-api/javadoc/com/firebase/simplelogin/SimpleLogin.html 获取完整的API参考.

See https://www.firebase.com/docs/java-simple-login-api/javadoc/com/firebase/simplelogin/SimpleLogin.html for the full API reference.

这篇关于Firebase:如何保持Android用户登录状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆