在Firebase登录上实施Smart Lock密码

时间:2018-02-20 11:49:43

标签: android google-api kotlin firebase-authentication google-smartlockpasswords

我一直在尝试在我的Firebase登录身份验证方法上实施Smart Locks,但我不得不承认,过去两天,谷歌文档绊倒了我。

有人能给我一个帮助吗?

这是我使用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)
    }
}

谢谢

1 个答案:

答案 0 :(得分:0)

在您的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
}