身份验证 - JavaScript - 注销问题

时间:2016-11-20 11:21:57

标签: javascript firebase firebase-authentication

我的问题需要一些帮助。

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

});

登录工作正常,但如果我尝试注销 - 用户可能正确注销 - 返回登录/注册网站,但我立即重定向到" event_list.html"仍然登录用户。

我的登录/注册网站是 - index.html

我想退出并输入其他数据 - 登录/通过。

另一个问题:如何从此脚本获取电子邮件变量并在其他JavaScript脚本中使用它?

我想做横幅 - 嗨(用户实际登录的邮件),欢迎回来并将其插入我的所有HTML网站。

2 个答案:

答案 0 :(得分:0)

我建议你看一下code in github 关于你的例子,我认为问题在于onAuthStateChanged方法(用户已经注销,因此auth状态已经改变,因此它被触发......)。
尝试使用signInWithEmailAndPassword解释here

通过其他方式,我会分开的问题,first make a search in SO,然后,如果没有回答满足我的需求,我会在新的问题中提出来!

答案 1 :(得分:0)

我也是Firebase的新手,但firebase.auth()。signOut()应该为你做的伎俩 并且为了识别用户是否已登录,您可以使用onAuthStateChanged事件。如果传递的参数为null,这意味着如果您具有有效值,则用户将被注销,那么您可以从中解析该用户

ex :(我没试过,但这应该有效)

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();
});
相关问题