Google login uses same account everytime users login

前端 未结 13 1777
无人共我
无人共我 2020-12-09 11:22

I use OAuth to let users sign in to the android app via Google account. When the user taps the Google login button for the first time, it produces a dialog to choose the acc

相关标签:
13条回答
  • 2020-12-09 12:04

    If you are having any logout functionality. Put the below code just before your logout code:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    
           CookieManager.getInstance().removeAllCookies(null);
           CookieManager.getInstance().flush();
    }
    else
    {
           CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(activity);
           cookieSyncMngr.startSync();
           CookieManager cookieManager=CookieManager.getInstance();
           cookieManager.removeAllCookie();
           cookieManager.removeSessionCookie();
           cookieSyncMngr.stopSync();
           cookieSyncMngr.sync();
    }
    

    Hope this will help.

    EDIT: you can also try replacing you storeDatainSharedPreferences method with below code:

    private void storeDatainSharedPreferences() {
    try {
    
        SharedPreferences.Editor editor = getSharedPreferences(NEW_PREFS, MODE_PRIVATE).edit();
        editor.putString("CurrentUsername", dataStore.getCurrentUserName());
        editor.putString("CurrentUserId", dataStore.getCurrentUserID());
        editor.commit();
        if(mGoogleApiClient!=null)
              mGoogleApiClient.disconnect();
    }
    catch (NoSuchElementException e)
    {
        new AlertDialog.Builder(LoginActivity.this).setMessage("There was an error whil logging in")
                .setTitle("Little Problem here!").setPositiveButton("Retry", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent=new Intent(LoginActivity.this,LoginActivity.class);
                removeDatainSharedPreferences();
                mGoogleApiClient.disconnect();
                startActivity(intent);
            }
        }).show();
    }}
    

    Explanation : For First solution : It is the code to clear all Cookies related to your application. This can be help full in your situation because you want to clear the previous data which might be stored in Cookies.

    Explanation : For Second solution : It is the code which disconnects the mGoogleApiClient. which will be helpful because You have stored the data in sharedPref now you don't need mGoogleApiClient.

    0 讨论(0)
  • 2020-12-09 12:08

    Signout your user using:

     Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        // ...
                    }
                });
    
    0 讨论(0)
  • 2020-12-09 12:09

    Just add this signIn() method after getting data from the intent.

    Intent signInIntent=Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    mGoogleApiClient.clearDefaultAccountAndReconnect(); 
    
    0 讨论(0)
  • 2020-12-09 12:10

    You are not revoking user by sign out them use the following to achieve what you need :

    Put this code in any activity and call it in sign out button click :

    private void signOut() {
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    // ...
                }
            });
    

    }

    write this in onCreate() :

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API)
                .build();
    

    override this in onStart() :

     @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }
    

    Reference Link : Google Developer Page

    0 讨论(0)
  • 2020-12-09 12:17

    The simplest way is to logout the client after you handle your result (onActivityResult). Just make sure that the client is connected before you trigger the logout. Here's a snippet of my code:

    private void handleGoogleLoginResult(Intent data) {
        if (data != null) {
            // get result from data received
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            int statusCode = result.getStatus().getStatusCode();
            if (result.isSuccess() && result.getSignInAccount() != null) {
                // Signed in successfully
    
            } 
            // logout
            if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                Auth.GoogleSignInApi.signOut(mGoogleApiClient);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-09 12:18

    You needed to log out the user from google client.

    public void signOut() {
       mAuth.signOut();
       mGoogleSignInClient.signOut();
       LoginManager.getInstance().logOut();
    }
    

    Where

    private FirebaseAuth mAuth;
    private GoogleSignInClient mGoogleSignInClient;
    

    and

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    mAuth = FirebaseAuth.getInstance();
    
    0 讨论(0)
提交回复
热议问题