Firebase Auth UI注销后,如何禁用密码自动登录请求的智能锁定? [英] How can I disable smart lock for passwords auto login request after Firebase Auth UI sign out?

查看:62
本文介绍了Firebase Auth UI注销后,如何禁用密码自动登录请求的智能锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个活动的Android应用程序,以尝试遵循Google的建议.我正在使用FirebaseAuth用户界面进行身份验证,该用户界面显然使用智能密码锁"将凭据保存到您的Google帐户中.我的退出功能如下:

I'm building a single activity Android app in an attempt to follow Google's recommendations. I'm using FirebaseAuth UI for authentication which apparently uses 'Smart Lock for Passwords' to save the credentials into your google account. My sign out function looks like this:

private fun signOutUser(){
    AuthUI.getInstance()
            .signOut(this)
            .addOnCompleteListener {
                Timber.i("Sign out completed")
            }
    sharedViewModel.setUser(null)
}

但是,注销完成后,UI会立即启动用户登录过程,如果使用Smart Lock for Passwords,则将弹出一个对话框.这使用户无法选择其他帐户.在FirebaseAuth UI的github帐户中,Google提到了这个问题:

However, once the sign out finishes, the UI immediately starts the user sign-in process, which with Smart Lock for Passwords means that a dialog pops up. This stops users from being able to pick another account. In the github account for FirebaseAuth UI, Google mentions this issue saying:

必须指示密码智能锁定"禁用自动登录,以防止自动登录循环阻止用户切换帐户."

"Smart Lock for Passwords must be instructed to disable automatic sign-in, in order to prevent an automatic sign-in loop that prevents the user from switching accounts."

他们的建议代码是:

public void onClick(View v) {
  if (v.getId() == R.id.sign_out) {
    AuthUI.getInstance()
        .signOut(this)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            public void onComplete(@NonNull Task<Void> task) {
                // user is now signed out
                startActivity(new Intent(MyActivity.this, SignInActivity.class));
                finish();
            }
        });
    }
}

但是由于我只有一个活动,所以无法执行startActivity.

But since I have only one activity I cannot do a startActivity.

所以我的问题是如何在用户注销后如何防止Smart Lock for Passwords尝试重新登录?

So my question is how can I prevent Smart Lock for Passwords from attempting to re-login after a user signs out?

以下是与我相关的身份验证代码的其余部分:

Here is the rest of my auth code if it's relevant:

override fun onStart() {
    super.onStart()

    // Enable Auth listener
    startAuthListener()

    // If user is not logged in, start the login process
    if(!sharedViewModel.isUserAuthenticated()){
        startLoginProcess()
    }

}

private fun initializeAuthListener() {
    mAuthStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
        if (null != firebaseAuth.currentUser) {
            // User is authenticated
            // user = firebaseAuth.currentUser
            sharedViewModel.setUser(firebaseAuth.currentUser)
            //refresh all data by calling getAllCollections, getAllPois
            sharedViewModel.refreshLocalCacheData()

        } else {
            // User is not signed in so kick off FirebaseUI login
            startLoginProcess()
        }
    }
}

private fun startAuthListener(){
    authService.addAuthStateListener(mAuthStateListener)
}

private fun startLoginProcess(){
    val providers = Arrays.asList(
            AuthUI.IdpConfig.EmailBuilder().build(),
            AuthUI.IdpConfig.GoogleBuilder().build())

    // Create and launch sign-in intent
    startActivityForResult(
            AuthUI.getInstance()
                    .createSignInIntentBuilder()
                    .setAvailableProviders(providers)
                    .build(),
            RC_SIGN_IN)
}

推荐答案

如果您不想为Firebase用户界面禁用google智能锁,则可以使用

If you wan't to disable google smart lock for your firebase UI then you can just use the

.setIsSmartLockEnabled(boolean)

.setIsSmartLockEnabled(boolean)

的属性

AuthUI.getInstance().createSignInIntentBuilder()

AuthUI.getInstance().createSignInIntentBuilder()

示例:

private final Intent signIn = AuthUI.getInstance()
        .createSignInIntentBuilder().setIsSmartLockEnabled(false)
        .setAvailableProviders(providers)
        .build();

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    if (auth.getCurrentUser() != null) 
    {
        //User is already loged in
        //show the main page

    } else startActivityForResult(signIn, RC_SIGN_IN); 
}

在这里,我使用了一个全局变量来存储Intent,以便在需要时可以重用它.但这并不总是必要的.

Here I have used a global variable to store the Intent so that I an reuse it if needed. But this is not always necessary.

这不是解决此问题的正确方法,但是禁用智能锁定只是禁用用户可能拥有的功能.因此,更好的解决方案是使用智能锁.

This is not the correct way to approach the problem this will work but disabling smart lock is just disabling a feature that your users could have had. So a better solution would be to use smart lock.

要在使用智能锁时注销用户,您应该添加:

To sign out a user while using smart lock you should add :

Credentials.getClient(this).disableAutoSignIn();

Credentials.getClient(this).disableAutoSignIn();

您的登出功能. 这将防止智能锁定在注销后立即重新登录用户.

to your sign Out function. this will prevent smart lock from immediately re-logging in the user once signed out.

这篇关于Firebase Auth UI注销后,如何禁用密码自动登录请求的智能锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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