Firebase:如何保持Android用户登录?

前端之家收集整理的这篇文章主要介绍了Firebase:如何保持Android用户登录?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Firebase SimpleLogin启用电子邮件/密码身份验证.创建用户和后续登录都是正常的.但是,每当我离开应用程序(即使只有几秒钟),用户从未在我的退货时登录,即
authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler())...

始终返回空用户.

我没有通过API注销用户.此外,我已经在Firebase控制台中设置了用户登录的天数到21.

我已经在JS文档中看到了一个记住我的参数,但是我看不到任何与Android / Java相似的东西.

想知道我是否在文档中丢失任何内容,或者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 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,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方法首先检查是否有登录用户,如果没有,则执行登录.请注意,每次启动应用程序时,我看到的日志记录是….

>尝试登录Firebase用户
>没有用户登录.将登录
>用户成功登录到Firebase

如果我退出应用程序,即使是几秒钟,并返回 – 我看到相同的日志记录.

有一件事我注意到,对checkAuthStatus的调用不会占用任何用户凭据 – 我认为它只是检查本地登录用户

非常感激.

解决方法

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

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

原文链接:https://www.f2er.com/android/313355.html

猜你在找的Android相关文章