Search code examples
angularfirebasefirebase-authenticationangularfireangularfire2

How to disconnect from Google authentication after angularfire2/auth signOut?


I'm building a website using Angular and Firebase. I'm using Angularfire2 and AngularFireAuth libraries and GoogleAuthProvider (Google) as my authentication provider.

Here is my service code:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';

@Injectable()
export class AuthenticationService {
  user: Observable<firebase.User>;

  constructor(public afAuth: AngularFireAuth) {
    this.user = afAuth.authState;
  }

  login() {
    this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
  }

  logout() {
    this.afAuth.auth.signOut();
  }
}

When a user 'logs out' of my site, it works, but seems to 'cache' the Google authentication. So when they attempt to login a second time I run into a few problems:

  1. User is not allowed to choose another Google account to login with.
  2. User is automatically logged in...which seems like a security problem. Another user could come to the same shared PC, and login as the previous user and is not required to type in a password to do so.

What am I doing wrong? Is there a way I can stop angularfire2/auth from caching these credentials? Is there a way to log users out of Google when they log out of my site?


Solution

  • This is expected as they are 2 separate systems and each Auth state is stored separately. Firebase Auth signOut signs you out of Firebase. To sign out from Google, you have to go to google and sign out. If this is critical for your app, you can always on Firebase Auth sign out, redirect to the google logout page:

    auth.signOut().then(() => {
      window.location.assign('https://accounts.google.com/Logout');
    }, (error) => {
      console.log(error);
    });