Search code examples
androidcordovafirebase-authenticationgoogle-plus-signin

Logout/Disconenct from Google Plus (Ionic/Cordova - cordova-plugin-googleplus)


I am trying to Logout/Disconnect from Google Plus (I'm using cordova-plugin-googleplus https://github.com/EddyVerbruggen/cordova-plugin-googleplus/)

Here is what is happening: I login to Google Plus through my application successfully and a logout button shows up. If i click logout, it will logout correctly and everything works great! however If i open my application, login to Google Plus successfully and close the application completely, when i open the app again and try to logout it gives me an error: Please use login or trySilentLogin before disconnecting

coming from this code inside GooglePlus.java from the plugin:

    private void disconnect() {
    if (this.mGoogleApiClient == null) {
        savedCallbackContext.error("Please use login or trySilentLogin before disconnecting");
        return;
    }

    ConnectionResult apiConnect = mGoogleApiClient.blockingConnect();

    if (apiConnect.isSuccess()) {
        Auth.GoogleSignInApi.revokeAccess(this.mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        if (status.isSuccess()) {
                            savedCallbackContext.success("Disconnected user");
                        } else {
                            savedCallbackContext.error(status.getStatusCode());
                        }
                    }
                }
        );
    }
}

And here is my code home.ts :

Logout() {
    let env = this;
    this.googlePlus.disconnect().then(
        (msg) => {
              if(firebase.auth().currentUser){
                firebase.auth().signOut();
              }
              env.nativeStorage.remove('user');
        }).catch(
        (msg) => {
            alert('logout error: '+msg);
        })
    ;
  }

The code that keeps me signed in as it seems without the application being aware that its "signed in" is in the home.ts constructor:

this.user = this.afAuth.authState.switchMap(user => {
 if (user) 
 {
    return this.afs.doc<User>(`users/${user.uid}`).valueChanges()
 } else 
 {
    return Observable.of(null)
 }
 })

user is of type Observable:

   user: Observable<User>;

Even after closing the application and reopening it I can still see the changes being made to the database even though i can't logout!

I hope that clarified my problem.

Thank you


Solution

  • Got it! Using trySilentLogin. You will have to Implement it exactly like the implementation of login() and it will work beautifully!

    Also don't forget to add it to the constructor:

    this.yourfunctioname();
    

    and inside you wll have he googlePlus.trySilentLogin() code!