如何注销谷歌身份验证? [英] How to logout google authentication?

查看:267
本文介绍了如何注销谷歌身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将Google身份验证登录信息集成到我的应用程序中,但是一旦登录后,如果我注销自己的帐户,每次应用程序自动登录旧用户帐户时仍然如此.

I have integrated Google authenticate login in my app but after once login if I log out my account still every time app automatically sign in the old user account.

MainAcivity

MainAcivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    GoogleSignInClient mGoogleSignInClient;
    private FirebaseAuth mAuth;
    private int RC_SIGN_IN=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAuth = FirebaseAuth.getInstance();
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
         mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        if (account != null){
            String personName = account.getDisplayName();
            String personGivenName = account.getGivenName();
            String personFamilyName = account.getFamilyName();
            String personEmail = account.getEmail();
            String personId = account.getId();
            Uri personPhoto = account.getPhotoUrl();
            Intent i=new Intent(MainActivity.this,Welcome.class);
            i.putExtra("pn",personName);           
            i.putExtra("pe",personEmail);         

            startActivity(i);
        }
        findViewById(R.id.sign_in_button).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();
                break;
            // ...
        }
    }
    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }
    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);

            // Signed in successfully, show authenticated UI.
            Intent i=new Intent(MainActivity.this,Welcome.class);
            startActivity(i);
        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Log.w("SignInFailed", "signInResult:failed code=" + e.getStatusCode());
            Toast.makeText(MainActivity.this,"SignInFailed",Toast.LENGTH_SHORT).show();
        }
    }
}

欢迎电竞

public class Welcome extends AppCompatActivity {
    TextView textView,textView2;
    GoogleSignInClient mGoogleSignInClient;
    private GoogleApiClient mGoogleApiClient;

    private FirebaseAuth mAuth;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        textView=findViewById(R.id.textView);
        textView2=findViewById(R.id.textView2);
        button=findViewById(R.id.button);

        Intent iin= getIntent();
        Bundle b = iin.getExtras();
        if(b!=null)
        {
            String j =(String) b.get("pn");
            textView.setText(j);
            String k =(String) b.get("pe");
            textView2.setText(k);
        }
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        if(account == null){
            Intent i=new Intent(Welcome.this,MainActivity.class);
            startActivity(i);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


               signOut();             

            }
        });

    }
    private void signOut() {
        mGoogleSignInClient.signOut()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Intent i=new Intent(Welcome.this,MainActivity.class);
                        startActivity(i);
                    }
                });
    }
//   
}

我已经使用过有关退出的Google文档,但无法解决我的问题.我还没有发现其他人已经提出过的有用问题. 感谢您提供的任何帮助.

I have used google docs about sign out but I can't solve my problem. I haven't found any useful questions from already asked by others. I appreciate any help you folks can offer.

推荐答案

您还需要从GoogleSignInClientFirebaseAuth当前用户注销,如下所示:

You also need to sign out from GoogleSignInClient and FirebaseAuth current user, something like this:

 //sign out of the user and start login activity.
public void signOut() {
    signOutBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();
           GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(getContext(), gso);
           mGoogleSignInClient.signOut();
           FirebaseAuth.getInstance().signOut();
           startActivity(new Intent(getContext(), LoginActivity.class));
        }
    });
}

这篇关于如何注销谷歌身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆