dimanche 24 février 2019

Firebase signs out user after signing in

I have the problem that my firebase hosting website has an /account route which dynamically loads a content page, depending on if the user is signed-in or not. For detecting the users state, I am using firebase auth's onAuthStateChange callback:

firebase.auth().onAuthStateChanged((user) => {
    console.log("authstate changed:"+user);
    let pagereq = new XMLHttpRequest();
    if (user) {
        console.log(user.email);
        user.getIdTokenResult(true).then((idtokenres) => {
            if(idtokenres.claims.admin){
                console.log("user is admin");
            }
        });
        pagereq.open("GET","./account-management/management.html",true);
        logout.addEventListener("click",handleLogout());
    } else {
        logout.parentNode.removeChild(logout);
        pagereq.open("GET","./account-management/login/login-page.html",true);
    }
}

When the user is signed in a dynamically loaded login form appears and enables a sign in with an email and a password. The user now enters his credentials and hits 'sign in'. The content area reloads due to the callback and shows a console page. Here comes the problem. When the user signs in, onAuthStateChanged is called twice: first sucessful with the exact user-Object and second with user = null. The console page dynamically loads a script after the console page itself is loaded. In the script, there also is an onAuthStateChanged function which will be called normally. Here, the callback is called only once and user also is null. Why is that and how can I workaround this problem? I can only imagine why onAuthStateChanged in the /account page gets called twice(The dynamically loaded and the pages script both recieve the same callback), but not why the user-Object null at the second call and also null in the dynamically loaded script.




Aucun commentaire:

Enregistrer un commentaire