一次登录应用程序-FirebaseAuth [英] One time login in app - FirebaseAuth

查看:114
本文介绍了一次登录应用程序-FirebaseAuth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Firebase身份验证通过电话号码登录用户的应用程序.我想添加一项功能,以便用户只能进行一次登录,即,即使用户终止了该应用程序并再次启动它,也应该登录他.而且,我也不想添加注销功能. .为此,该怎么办?

I'm working on an app that uses Firebase authentication to sign in users through phone number. I want to add a functionality such that there is only one-time login for the user, i.e. even if the user kills the app and starts it again, he should be logged in. Also, I don't want to add a logout feature. What can be done for this?

推荐答案

最简单的方法是使用侦听器.假设您有两个活动,LoginActivityMainActivity.可以在LoginActivity中创建的侦听器应如下所示:

The simplest way to achieve this is to use a listener. Let's assume you have two activities, the LoginActivity and the MainActivity. The listener that can be created in the LoginActivity should look like this:

FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if (firebaseUser != null) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
};

这基本上意味着,如果用户已登录,请跳过LoginActivity并转到MainActivity.

This basically means that if the user is logged in, skip the LoginActivity and go to the MainActivity.

实例化FirebaseAuth对象:

FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

并开始监听您的onStart()方法中的更改,如下所示:

And start listening for changes in your onStart() method like this:

@Override
protected void onStart() {
    super.onStart();
    firebaseAuth.addAuthStateListener(authStateListener);
}

MainActivity中,您应该执行相同的操作:

In the MainActivity, you should do the same thing:

FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if (firebaseUser == null) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
        }
    }
};

这基本上意味着,如果用户未登录,请跳过MainActivity并转至LoginActivity.在此活动中,您应该执行与LoginActivity中相同的操作,应该开始侦听onStart()中的更改.

Which basically means that if the user is not logged in, skip the MainActivity and go to the LoginActivity. In this activity you should do the same thing as in the LoginActivity, you should start listening for changes in the onStart().

在这两个活动中,不要忘记在不再需要时删除监听器.因此,在您的onStop()方法中添加以下代码行:

In both activities, don't forget to remove the listener in the moment in which is not needed anymore. So add the following line of code in your onStop() method:

@Override
protected void onStop() {
    super.onStop();
    firebaseAuth.removeAuthStateListener(authStateListener);
}

这篇关于一次登录应用程序-FirebaseAuth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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