3

My Ionic 2 app as Google Authentication via Firebase and I have a logout button in the app calling the Firebase unauth() method but that only unauthenticates the Firebase reference and does not kill the Google OAuth session.

After pressing the logout button and pressing the login button again the user is automatically logged in (using the previous OAuth session) and I don't want that.

I need my logout button to also kill the Google OAuth session so when the login button is pressed again it prompts for the username and password once more. How can I achieve that?

Here's my code:

home.ts

import {Page} from 'ionic-angular';

@Page({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
    firebaseUrl: string;
    ref: Firebase;

    constructor() {
        this.firebaseUrl = 'https://xxx.firebaseio.com';
        this.ref = new Firebase(this.firebaseUrl);
    }

    login() {
        this.ref.authWithOAuthPopup("google", (error, authData) => {
            if (error) {
                console.log("Login Failed!", error);
            } else {
                console.log("Authenticated successfully with payload:", authData);
            }
        });
    }

    logout() {
        this.ref.unauth();
        console.log('Logout button clicked');
    }

}

home.html

<ion-navbar *navbar>
  <ion-title>
    Home
  </ion-title>
</ion-navbar>

<ion-content class="home">
  <button (click)="login()">Sign in with Google</button>
  <button (click)="logout()">Logout</button>
</ion-content>
nunoarruda
  • 2,679
  • 5
  • 26
  • 50

1 Answers1

0

The only working solution I've found was to make a JSONP asynchronous request to https://accounts.google.com/logout

This is a "dirty" trick and spits errors to the console but couldn't find any other working solution. Let me know if someone knows a better way.

Community
  • 1
  • 1
nunoarruda
  • 2,679
  • 5
  • 26
  • 50
  • I searched the same links as I can see you have. And it is expected that the OAuth session remains. You don't really want to log out the user from other Google services when clicking log out in your application. – henrikmerlander Mar 30 '16 at 11:31
  • In my case I do want to logout so I can be able to login again with a different Google account. – nunoarruda Mar 31 '16 at 00:30
  • I can understand it behaves this way, I wouldn't like my Facebook session to end just because i pressed log out in some other application. Glad you found a way to do it though! – henrikmerlander Mar 31 '16 at 06:29
  • I don't have the issue of logging out the user from other services since it's a hybrid mobile app contained in a web view. But if it was a website or a web app then yeah the user would be logged out from other services using the same login system. – nunoarruda Apr 03 '16 at 12:46