admin管理员组

文章数量:1328001

I'm using Firebase authentication with google accounts. Login process works fine, but I have a problem with logout. Everything seems fine when user clicks "logout" button, but when "login" button is clicked after logout, the previously signed in user is loged in automaticaly - no prompt for credentials. That happens even in incognito mode.

This is the code i'm using. All the munication with Firebase services happens here:

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

I'm using Firebase authentication with google accounts. Login process works fine, but I have a problem with logout. Everything seems fine when user clicks "logout" button, but when "login" button is clicked after logout, the previously signed in user is loged in automaticaly - no prompt for credentials. That happens even in incognito mode.

This is the code i'm using. All the munication with Firebase services happens here:

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

What can be the problem here? Thanks!

Share Improve this question edited Apr 16, 2018 at 17:59 IGOR LEVKOVSKY asked Apr 16, 2018 at 15:24 IGOR LEVKOVSKYIGOR LEVKOVSKY 1671 gold badge5 silver badges14 bronze badges 13
  • Are you sure you are going through the .signOut() success path? If, after the .signOut() has pleted successfully does firebase.auth() still return a logged in user object? (i.e. are you sure the logout was successful?). It might also help to show your login code, perhaps there's a problem there. – joelm Commented Apr 16, 2018 at 15:30
  • Why are you calling initializeApp during logout? That's typically something you do once when the page first loads. It doesn't make sense call it ever again after that point. – Doug Stevenson Commented Apr 16, 2018 at 15:32
  • @DougStevenson Because if I don't initialize it again, nothing works at all. – IGOR LEVKOVSKY Commented Apr 16, 2018 at 17:53
  • @joelm The proccess goes through successful path, I see taht sessionStorage bees clean. Same about loclaStorage. I've added the login code so you can see what happens from the beggining. – IGOR LEVKOVSKY Commented Apr 16, 2018 at 17:56
  • You should only have to call initializeApp once per page load. Anything else is not correct. – Doug Stevenson Commented Apr 16, 2018 at 18:17
 |  Show 8 more ments

2 Answers 2

Reset to default 4

I tried this and worked for me.

var provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({
  prompt: 'select_account'
});

Finally i got it. Working in cordova :

You need to logout the googlePlus as well. Please try below :

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

----------And for website try below : ----

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

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

本文标签: javascriptFirebase logout not workingStack Overflow