Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase logout not working

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 communication 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!

like image 753
IGOR LEVKOVSKY Avatar asked Feb 23 '26 04:02

IGOR LEVKOVSKY


1 Answers

I tried this and worked for me.

var provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({
  prompt: 'select_account'
});
like image 140
Anand_5050 Avatar answered Feb 25 '26 16:02

Anand_5050