且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

身份验证 - JavaScript - 注销问题

更新时间:2023-12-02 11:44:52

我也是Firebase的新手,但是firebase.auth()。signOut b $ b为了确定用户是否登录,可以使用 onAuthStateChanged 事件。如果传递的参数为null,这意味着如果你有一个有效的值用户注销,那么你可以解析用户从

前:(我还没有尝试过,但这应该工作)

  firebase.auth()。onAuthStateChanged(user => {
if(user){
console.log('User Logged in:'+ user);
window.location.href ='event_list.html'
} else {
console.log('User Not Logged in');
window.location.href ='index.html';
}
});

btnLogout.addEventListener('click',e => {
firebase.auth()。signOut();
});


I need some help with my problem.

firebase.auth().onAuthStateChanged(user => {
    if(user) {
    console.log('log in');
    window.location.href ='event_list.html'
    }});

btnLogout.addEventListener('click', e => {
    firebase.auth().signOut().then(function() {
        console.log("not log in");
        window.location.href = 'index.html';
    }, function(error) {
    //
    });;

});

Logging in works correctly but if I'm trying logout - user probably logout correctly - back to login/signup site but immediately I'm redirecting to "event_list.html" as still logged in user.

My sign in/sign up site is - index.html

I want logout and put other data - login/pass.

One other question: How can I get email variable from this script and use it in other JavaScript script?

I want do banner - Hi (mail of user actually log in), Welcome back and insert it to all my HTML sites.

I'm also new to Firebase but firebase.auth().signOut() should do the trick for you And in-order to identify if the user is logged in you can use the onAuthStateChanged event. If the passed parameter is null which means user is logged out if you have a valid value then you can resolve the user from that

ex: (I have not tried this but this should work)

firebase.auth().onAuthStateChanged(user => {
    if(user) {
        console.log('User Logged in :' + user );
        window.location.href ='event_list.html'
    }else{
        console.log('User Not Logged in');
        window.location.href = 'index.html';
    }
});

btnLogout.addEventListener('click', e => {
    firebase.auth().signOut();
});