简体   繁体   中英

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? Ican not detect why the user -Object null at the second call and also null in the dynamically loaded script.

尝试在handleLogout上删除() ,就像logout.addEventListener('click', handleLogout)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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