Firebase:如何让Android用户登录?

Nei*_*eil 16 android firebase firebase-security

我正在使用Firebase SimpleLogin来启用电子邮件/密码身份验证.创建用户和后续登录都可以正常工作.但是,每当我离开应用程序时(即使只持续几秒钟),用户从未在我的返回时登录,即...

authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler())...
Run Code Online (Sandbox Code Playgroud)

始终返回null用户.

我没有通过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);
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

用户登录....

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");
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

我注意到的一件事是对checkAuthStatus的调用不会获取任何用户凭据 - 我认为它只是检查任何本地登录的用户?

非常感激.

Rob*_*rco 11

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

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


Lee*_*nah 10

另一种方法 - 在您的代码中尝试此代码onCreate:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
    Intent i = new Intent(LoginActivity.this, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
} else {
    // User is signed out
    Log.d(TAG, "onAuthStateChanged:signed_out");
}
Run Code Online (Sandbox Code Playgroud)

这将使用户通过直接将用户带到Main活动而不停止注册活动来保持登录.因此,除非用户单击"注销",否则用户将登录.

  • 我的回答将通过将用户直接带到主活动而不在注册活动中停止来保持用户登录。所以除非用户点击退出,否则用户将被登录。 (2认同)

小智 6

如果用户已经登录,您可以通过使用此方法转义日志页面来执行此操作。

private FirebaseAuth auth;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    auth = FirebaseAuth.getInstance();

    if (auth.getCurrentUser() != null) {
        startActivity(new Intent(Login_Activity.this, Home.class));
        finish();
    }
    setContentView(R.layout.activity_login_);
Run Code Online (Sandbox Code Playgroud)


Ole*_*sov 5

正确的方法是使用oAuth身份验证:

1. The user logs in.
2. You generate an access token(oAuth2).
3. Android app saves the token locally.
4. Each time the comes back to the auth, he can use the token to to log in, unless the token has been revoked by you, or he changed his
password.
Run Code Online (Sandbox Code Playgroud)

幸运的是,firebase对此提供了开箱即用的支持,docs:

https://www.firebase.com/docs/security/custom-login.html https://www.firebase.com/docs/security/authentication.html

  • 这很酷,我的人,firebase 的人应该在他们的文档的某个地方添加对这个特定点的引用。 (2认同)