firebase auth delayed on refresh

后端 未结 4 1560
醉话见心
醉话见心 2021-02-05 20:50

Hard refreshes on my SPA React/Firebase application does not maintain auth state on immediate execution of a function. I have a workaround, but it\'s sketchy.

My react

4条回答
  •  日久生厌
    2021-02-05 21:34

    Okay. So, I was able to solve this by utilizing the localStorage variable that firebase provides to store the user information.

    function (nextState, replace) {
        if (!firebase.auth().currentUser) {
            let hasLocalStorageUser = false;
            for (let key in localStorage) {
                if (key.startsWith("firebase:authUser:")) {
                    hasLocalStorageUser = true;
                }
            }
            if (!hasLocalStorageUser) {
                console.log('Attempting to access a secure route. Please authenticate first.');
                replace({
                    pathname: '/login',
                    state: { nextPathname: nextState.location.pathname }
                });
            }
        }
    };
    

提交回复
热议问题