【问题标题】:How to make a user sign out in google session firebase?如何让用户在 google session firebase 中退出?
【发布时间】:2018-05-03 01:45:40
【问题描述】:

这是我的代码

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

    googleApiClient = GoogleApiClient.Builder(this)
            .enableAutoManage(this){}
            .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
            .build()

}

override fun onClick(view: View?) {
    when (view?.id) {
        R.id.google_sign_in_button -> {
            Log.i(TAG, "Trying Google LogIn.")
            googleLogin()
        }

    }
}

private fun googleLogin() {

    val signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient)
    startActivityForResult(signInIntent, 1)
}


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

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == 1) {
        val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)

        if (result.isSuccess) {
            // Google Sign In was successful, authenticate with Firebase
            firebaseAuthWithGoogle(result.signInAccount!!)
        } else {
            Toast.makeText(this, "Some error occurred.", Toast.LENGTH_SHORT).show()
        }
    }
}

private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {

    val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
    firebaseAuth?.signInWithCredential(credential)?.addOnCompleteListener(this) { task ->

        if (task.isSuccessful) {
            // Sign in success, update UI with the signed-in user's information
            startActivity(Intent(this, prueba::class.java))


        } else {
            // If sign in fails, display a message to the user.

        }
    }
}

当会话开始时,它会将我带到另一个活动。 问题是我如何从其他活动中关闭该会话。 我想关闭 google 而不是 firebase 的会话,以便它询问我要重新开始使用哪个帐户

【问题讨论】:

    标签: android firebase kotlin firebase-authentication


    【解决方案1】:

    试试这个

     mGoogleSignInClient.signOut();
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                                .requestIdToken(getString(R.string.default_web_client_id))
                                .requestEmail()
                                .build();
                        mGoogleSignInClient = GoogleSignIn.getClient(SettingsActivity.this, gso);
    

    这将使您退出 Google,但要让用户返回登录页面,只需使用

    startActivity(new Intent(WhateverActivityItsOn.this, BacktoSignInPageActivity.class));
    

    【讨论】:

      猜你喜欢
      • 2017-07-23
      • 2017-08-08
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      相关资源
      最近更新 更多