4

我正在使用Angular和Firebase構建網站。我使用Angularfire2和AngularFireAuth庫和GoogleAuthProvider(Google)作爲我的身份驗證提供程序。如何在angularfire2/auth signOut之後從Google身份驗證中斷開連接?

這裏是我的服務代碼:

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(); 
    } 
} 

當用戶註銷'我的網站,它的工作原理,但似乎「緩存」的谷歌認證。因此,當他們嘗試第二次登錄時,我遇到了幾個問題:

  1. 用戶不允許選擇其他Google帳戶進行登錄。
  2. 用戶自動登錄...這似乎是一個安全問題。另一個用戶可以訪問同一個共享PC,並以前一個用戶身份登錄,並且不需要輸入密碼即可。

我在做什麼錯?有沒有辦法阻止這些證書緩存angularfire2/auth?有沒有辦法在用戶註銷我的網站時將用戶註銷?

回答

2

這是預期的,因爲它們是2個獨立的系統,每個身份驗證狀態都單獨存儲。 Firebase身份驗證signOut標誌您退出Firebase。要從Google退出,您必須前往Google並退出。如果這是你的應用程序的關鍵,你總是可以在火力地堡驗證註銷,重新定向到谷歌退出頁面:

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

我很欣賞你的反應,並同意這是 「作爲設計的」,但我仍然相信這是一個不幸的用戶體驗。我寧願選擇讓我使用Google憑據登錄,但不能同時登錄Google。 – szaske

0

作爲一個建議,你可以通過下面的「解決方案」在這個link做更謹慎。 我在我的應用程序中使用它,以防用戶無權訪問數據庫,但可以在任何地方添加iframe。

註銷服務:

function LogoutService($location){ 
     this.logOut = function(myAuth){ 
      myAuth.$signOut().then(() => { 

       $location.path("/logout"); 

      }, (error) => { 
       console.log(error); 
      }); 
     }; 
    }; 

路由器:

.when('/logout', { 
      templateUrl : "login/logout.html" 
     }) 

logout.html:

<div class="container" > 

<p >{{logUser.unauthUser}} unauthorized. Try again.</p> 

<iframe id="logoutframe" src="https://accounts.google.com/logout" style="display: none"></iframe> 

</div>