简体   繁体   English

为什么 firebase.auth().currenUser 为空?

[英]Why is firebase.auth().currenUser null?

EDIT2:编辑2:

CODE:代码:

<script>
    console.log("CURRENT USER:"+firebase.auth().currentUser);

    var inElements = document.getElementsByClassName('authIn');
    var outElements = document.getElementsByClassName('authOut');

    if (firebase.auth().currentUser == null) {

        for(var i=0; i < inElements.length; i++) { 
            inElements[i].style.display = "none";
        }
        for(var i=0; i < outElements.length; i++) { 
            outElements[i].style.display = "inline-block";
        }
    }
    else {

        for(var i=0; i < inElements.length; i++) { 
            inElements[i].style.display = "inline-block";
        }
        for(var i=0; i < outElements.length; i++) { 
            outElements[i].style.display = "none";
        }
    }
</script>

SITUATION:情况:

I log in, firebase.auth().currentUser is null.我登录, firebase.auth().currentUser 为空。


QUESTION:题:

Why and how do I fix it ?为什么以及如何修复它?


LOGIN CODE:登录代码:

<script>


            firebase.auth().signInWithEmailAndPassword(email, password ).then( authData => {
             //A LOT OF CODE FOR DIFFERENT SITUATIONS


            },function(error) { 

                var errorCode = error.code;
                var errorMessage = error.message;
                localStorage.setItem('error_msg_local', "Unknown user or password");
                window.location.href="/users/login";
                console.log("Login Failed: ", error);

            });


</script>

In the login code, I show different kinds of error messages depending on how the authentication went.在登录代码中,我根据身份验证的方式显示不同类型的错误消息。 There is also some code to manage verified and unverified accounts.还有一些代码来管理已验证和未验证的帐户。

You need to set your authdata variable so that it reflects whether the user is logged in or not.您需要设置authdata变量,以便它反映用户是否已登录。

You can run this code on initialization which creates an observer on the firebase Auth object.您可以在初始化时运行此代码,它会在 firebase Auth 对象上创建一个观察者。 Anytime a logout/login change happens the authdata variable will be updated accordingly.. authdata发生注销/登录更改时, authdata变量都会相应地更新..

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    authdata = user;
  }
  else {
   authdata = null;
  }
});

Alternatively if for some reason you don't want the observer approach you can simply set your authdata like so:或者,如果由于某种原因你不想要观察者方法,你可以简单地设置你的authdata像这样:

authdata = firebase.auth().currentUser;

If the user is not logged in authdata will be set to null and your ejs should update accordingly..如果用户未登录 authdata 将设置为 null 并且您的ejs应相应更新..

I'm assuming your ejs is running on the client.我假设您的ejs正在客户端上运行。 If it's not then your ejs template won't pick up on these changes.如果不是,那么您的ejs模板将不会接受这些更改。

You'll want to load the ejs script client side and run your template from there.您需要加载ejs脚本客户端并从那里运行您的模板。

See: EJS Client Side Support for reference.请参阅: EJS 客户端支持以供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM