首页 > 解决方案 > 如何让用户在 google session firebase 中退出?

问题描述

这是我的代码

    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 的会话,以便它询问我要重新开始的帐户

标签: androidfirebasekotlinfirebase-authentication

解决方案


尝试这个

 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));

推荐阅读