Google和Facebook在Kotlin中使用Firebase进行身份验证的问题

时间:2019-03-06 16:24:35

标签: firebase firebase-authentication google-authentication facebook-authentication

我正在创建一个具有Google和Facebook身份验证的用户登录页面,并且遵循从Firebase找到的所有文档,当我创建Google Login时,一切正常,但是当我添加Facebook登录时,它仅显示进度栏和什么都不做。希望可以有人帮帮我。我不确定是否是因为我没有正确处理“ onActivityResult”。

      firebaseAuth=FirebaseAuth.getInstance()

            callbackManager = CallbackManager.Factory.create()
            login_button_facebook.setReadPermissions("email")
            //var btnLoginFacebbok = findViewById<Button>(R.id.login_button_facebook)
            login_button_facebook.setOnClickListener({
                    SignInFacebook()
            })

        }

        private fun SignInFacebook() {

           login_button_facebook.registerCallback(callbackManager, object :FacebookCallback<LoginResult>{
               override fun onSuccess(result: LoginResult?) {
                   if (result != null) {
                       handleFacebookAccessToken(result.accessToken)
                   }


               }


               override fun onCancel() {
                   TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
               }

               override fun onError(error: FacebookException?) {

               }

           })


        }


        private fun handleFacebookAccessToken(accessToken: AccessToken?) {
            Log.d(String.toString(), "handleFacebookAccessToken:" + accessToken)
            //obtener credenciales
            val credential=FacebookAuthProvider.getCredential(accessToken!!.token)
            firebaseAuth!!.signInWithCredential(credential!!)
            .addOnSuccessListener(this) {result->
                   val logged_Email= result.user.email
                  // val logged_Activity=Intent(this@MainActivity, WelcomeUser::class.java)
                Toast.makeText(this, "ENTRÓ AQUÍ"+logged_Email, Toast.LENGTH_LONG).show()

               }
               .addOnFailureListener(this){ e->
                   Toast.makeText(this, ""+e.message, Toast.LENGTH_LONG).show()
               }


        }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode==RC_SIGN_IN){
       // val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)

       val result=Auth.GoogleSignInApi.getSignInResultFromIntent(data)
        if(result.isSuccess){
            val account=result.signInAccount
            val idToken= account!!.idToken

            val credential=GoogleAuthProvider.getCredential(idToken,null)
            firebaseAuthWithGoogle(credential)
               // onStart()
            val intento1 = Intent(this, WelcomeUser::class.java)
            startActivity(intento1)

        }
           //this is Facebook part
        callbackManager!!.onActivityResult(resultCode, resultCode, data )



    }


}

0 个答案:

没有答案