首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Firebase登录时实现智能锁密码

在Firebase登录时实现智能锁密码
EN

Stack Overflow用户
提问于 2018-02-20 19:49:43
回答 1查看 1.1K关注 0票数 2

我一直试图在我的Firebase Login Auth方法上实现Smart Locks,但我不得不承认,在过去的两天里,google docs把我绊倒了。

有谁能帮我一下吗?

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

代码语言:javascript
复制
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)
    }
}

谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-05-30 11:16:59

您可以在您的LoginActivity中添加以下内容:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48884669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档