Ad

How To Sign In And Sign Out From Google Within One Activity

- 1 answer

I'm beginner in Java and Android programming. I'm trying to create Login system with Google on my app. What I want to do is to switch sign in and sign out button in an activity. However, once I sign in successfully, I can never sign out from Google. Even if I uninstalled the app, I couldn't sign out.

Here is my code.

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    mSignInButton = findViewById(R.id.sign_in_button);
    mSignInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signIn();
        }
    });

    mSignOutButton = findViewById(R.id.sign_out_button);
    mSignOutButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signOut();
        }
    });


@Override
protected void onStart() {
    // Check for existing Google Sign In account, if the user is already signed in
    // the GoogleSignInAccount will be non-null.
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    updateUI(account);
    super.onStart();
}


@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 signIn() {
    Intent intent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(intent, RC_SIGN_IN);
}


private void signOut() {
    mGoogleSignInClient.signOut();
}


private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        String idToken = account.getIdToken();

        // Send the token to the API endpoint and validate


        // Signed in successfully, show authenticated UI.
        updateUI(account);
    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        updateUI(null);
    }
}


private void updateUI(GoogleSignInAccount signInAccount) {
    if (signInAccount != null) {
        mSignInButton.setVisibility(View.GONE);
        mSignOutButton.setVisibility(View.VISIBLE);
    } else {
        mSignInButton.setVisibility(View.VISIBLE);
        mSignOutButton.setVisibility(View.GONE);
    }
}

How can I sign out correctly and update the UI within one activity?

Ad

Answer

try updating your code based on the docs on how to sign out and disconnect. this will wait until the user is signed out from the server and updates the app UI.

private void signOut() {
    mGoogleSignInClient.signOut()
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    updateUI(null);
                }
            });
}
Ad
source: stackoverflow.com
Ad