在Firebase登录中实现智能锁定密码



我一直在尝试在我的firebase登录验证方法上实现智能锁,但是我必须承认过去两天,Google Docs绊倒了我。

有人可以给我帮助吗?

这是我带有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)
    }
}

谢谢

在您的登录时您可以添加以下内容:

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
}

相关内容

  • 没有找到相关文章

最新更新