在 Firebase 登录上实施 Smart Lock 密码

Posted

技术标签:

【中文标题】在 Firebase 登录上实施 Smart Lock 密码【英文标题】:implement Smart Lock Password on Firebase Login 【发布时间】:2018-07-30 18:43:40 【问题描述】:

我一直在尝试在我的 Firebase 登录身份验证方法上实现智能锁,但我不得不承认,过去两天,谷歌文档让我失望了。

谁能帮帮我?

这是我在 Google 上的身份验证登录

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build()

    mGoogleApiClient = GoogleApiClient.Builder(this)
            .enableAutoManage(this, GoogleApiClient.OnConnectionFailedListener  )
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build()

    loginGoogleBtn.setOnClickListener 
        val signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
        startActivityForResult(signInIntent, GOOGLE_SIGN_IN)
    




override fun onStart() 
    super.onStart()
    // Check if user is signed in (non-null) and update UI accordingly.
    val currentUser = auth.currentUser
    updateUI(currentUser)


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) 
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == GOOGLE_SIGN_IN) 
        val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
        if (result.isSuccess) 
            if (result.signInAccount != null) 
                val account = result.signInAccount!!
                val credential = GoogleAuthProvider.getCredential(account.idToken, null)
                firebaseLogin(credential)
            

         else 
            Log.e("Error", "Could not signin google")
        
     else 
        mCallbackManager.onActivityResult(requestCode, resultCode, data)
    

谢谢

【问题讨论】:

智能锁是什么意思?你的意思是例如人体检测、可信地点、可信声音等? 嗨。对不起,如果我不清楚。我的意思是这个developers.google.com/identity/smartlock-passwords/android。如果用户激活了智能锁,则使用 gmail 自动登录 @TomAlabaster ,你知道它是什么吗? 您可能想在此处查看 Firebase 身份验证 UI 代码:github.com/firebase/FirebaseUI-Android。他们实施了 Smart Lock。 @creativecreatorormaybenot 你知道我的代码在哪里可以实现他们提供的东西吗?这是: startActivityForResult( AuthUI.getInstance() .createSignInIntentBuilder() .setIsSmartLockEnabled(false) .build(), RC_SIGN_IN); 【参考方案1】:

在你的 LoginActivity 中你可以添加这个:

private val smartLockClient by lazy 
    Credentials.getClient(this)


private val firebaseAuth by lazy 
    FirebaseAuth.getInstance()


override fun onCreate(savedInstanceState: Bundle?) 
    super.onCreate(savedInstanceState)
    setContentView(R.layout.login) // put a id "container" in the root ViewGroup

    // Run Google Smart Lock
    smartLock()


private fun smartLock() 
    container.visibility = View.GONE

    val credentialRequest = CredentialRequest.Builder()
            .setPasswordLoginSupported(true)
            .setIdTokenRequested(true)
            .setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.FACEBOOK)
            .build()
    smartLockClient.request(credentialRequest).addOnCompleteListener  task ->

        if (task.isSuccessful) 
            // See "Handle successful credential requests"
            val credential = task.result?.credential ?: return@addOnCompleteListener
            when (credential.accountType) 
                null -> 
                    firebaseAuth.signInWithEmailAndPassword(credential.id, credential.password
                            ?: "")
                
                IdentityProviders.GOOGLE -> 

                    GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                            .setAccountName(credential.id)
                            .requestIdToken(getString(R.string.default_web_client_id))
                            .build()
                            .let  GoogleSignIn.getClient(this, it) 
                            .silentSignIn()
                            .addOnSuccessListener 
                                val authCredential = GoogleAuthProvider.getCredential(it.idToken, null)
                                firebaseAuth.signInWithCredential(authCredential)
                                        .addOnSuccessListener  onSignInSuccess() 
                                        .addOnFailureListener  onSignInFailure() 
                            
                            .addOnFailureListener  onSignInFailure() 
                
            
        
    


private onSignInSuccess()
    // navigate to your main page or something else


private onSignInFailure()
    // smart lock fail, so show the user the buttons "Sign in with Google", "Sign in with Email", "Sign in With Facebook", etc.
    container.visibility = VISIBLE

【讨论】:

以上是关于在 Firebase 登录上实施 Smart Lock 密码的主要内容,如果未能解决你的问题,请参考以下文章

在 Firebase 中实施自定义身份验证的工作流程是啥?

在服务上实施 INotifyCollectionChanged 或 INotifyPropertyChanged 可以吗?

在 Exchange ActiveSync 客户端上实施配置

在亚马逊美食评论数据集上实施的朴素贝叶斯问题

yaffs2在am335x上实施

在 Android 12 上实施 STILL_IMAGE_CAPTURE_SECURE