Firebase注销无法正常工作

时间:2018-04-16 15:24:13

标签: javascript firebase firebase-authentication

我正在使用Google帐户使用Firebase身份验证。登录过程工作正常,但我有登出问题。当用户点击“注销”按钮时,一切似乎都很好,但是当注销后单击“登录”按钮时,先前登录的用户自动挂起 - 没有提示凭据。即使在隐身模式下也会发生这种情况。

这是我正在使用的代码。与Firebase服务的所有通信都在此处进行:

function auth() {	
	// Initialize Firebase;
	firebase.initializeApp(settings);
	var provider = new firebase.auth.GoogleAuthProvider();
	
	firebase.auth().signInWithPopup(provider).then(function(result) {
		// This gives you a Google Access Token. You can use it to access the Google API.
		var token = result.credential.accessToken;
		sessionStorage.setItem('tokenK', token);
		// The signed-in user info.
		var user = result.user;
		var tempName = user.displayName.split(" ");
		var fullName = tempName[0].charAt(0).toUpperCase() + tempName[0].toLowerCase().substring(1, tempName[0].length) + 
		" " + tempName[1].charAt(0).toUpperCase() +tempName[1].toLowerCase().substring(1, tempName[1].length);
		sessionStorage.setItem('displayName', fullName);
		sessionStorage.setItem('userName', user.email);
	}).catch(function(error) {
		// Handle Errors here.
		var errorCode = error.code;
		var errorMessage = error.message;
		// The email of the user's account used.
		var email = error.email;
		// The firebase.auth.AuthCredential type that was used.
		var credential = error.credential;
		console.log(error);
	});
}

function logOut(){  
    firebase.initializeApp(settings);
    var dataJ = JSON.stringify(sessionStorage.getItem('userName'));
    var xhttp = new XMLHttpRequest();

    firebase.auth().signOut().then(function() {
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 204) {
                sessionStorage.removeItem('tokenK');
                sessionStorage.removeItem('displayName');
                sessionStorage.removeItem('userName');
                sessionStorage.removeItem('role');
                sessionStorage.removeItem('school');
                sessionStorage.removeItem('grade');
                window.open('index.html', '_self');                 
            }                   
        };
        xhttp.open("POST", settings.protocol + "://" + settings.host + ":" + settings.port + "/api/Login/SignOut", true);
        xhttp.setRequestHeader("Content-Type", "application/json");
        xhttp.setRequestHeader("Token", sessionStorage.getItem('tokenK'));  
        xhttp.send(dataJ);
    }).catch(function(error) {
        console.log(error);
    }); 
}

这可能是什么问题? 谢谢!

2 个答案:

答案 0 :(得分:1)

我尝试过并为我工作。

var provider = new firebase.auth.GoogleAuthProvider(); provider.setCustomParameters({   提示:“ select_account” });

答案 1 :(得分:0)

最后我明白了。在科尔多瓦工作:

您还需要注销googlePlus 。请尝试以下:

doLogout(){
    var promise =  new Promise((resolve, reject) => {
      if(firebase.auth().currentUser){
        this.afAuth.auth.signOut();
        if (this.platform.is('cordova')) {
          this.googlePlus.logout();
        }
        resolve(true);
      }
      else {
        reject();
      }
    });
    return promise;    }

---------- 对于网站,请尝试以下内容: ----

    var provider = new firebase.auth.GoogleAuthProvider(); 

provider.setCustomParameters({ prompt: 'select_account' });