问题
I'm using Firebase authentication with google accounts. Login process works fine, but I have a problem with logout. I successfuly log out of firebase, but not from google. That means that the user remains logged in to google. How can I log out from both of them?
That is my code:
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);
});
}
And
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);
});
}
I've seen this post, speaking about similar issue on android, but can't find anything for JS.
回答1:
Normally this is expected behavior. Sign out from firebase is unrelated to sign out from google. User would need to explicitly sign out from google. However, if you know your application will be used on a share device you can do the following:
// Sign out from firebase.
firebase.auth().signOut().then(function() {
// Redirect to google sign out.
window.location.assign('https://accounts.google.com/logout');
}).catch(function(error) {
// Error occurred.
});
回答2:
mGoogleSignInClient.signOut().addOnCompleteListener(ClassName.this, new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { Toast.makeText(ClassName.this, "logged out", Toast.LENGTH_SHORT).show(); } });
来源:https://stackoverflow.com/questions/50320147/logout-from-google-after-login-out-from-firebase