Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase onAuthStateChanged在登录时被调用两次_Javascript_Reactjs_Firebase_Redux_Firebase Authentication - Fatal编程技术网

Javascript Firebase onAuthStateChanged在登录时被调用两次

Javascript Firebase onAuthStateChanged在登录时被调用两次,javascript,reactjs,firebase,redux,firebase-authentication,Javascript,Reactjs,Firebase,Redux,Firebase Authentication,我是firebase的新手。我正在尝试使用React+Redux登录。当我单击“登录”时,它将让我登录一次,然后再次调用onAuthStateChanged,其中res为null,这将使我退出 以下是我的Firebase服务: const initFirebase = () => { firebase.initializeApp(config.firebase) firebase.auth().onAuthStateChanged((res) => {

我是firebase的新手。我正在尝试使用React+Redux登录。当我单击“登录”时,它将让我登录一次,然后再次调用onAuthStateChanged,其中
res
为null,这将使我退出

以下是我的Firebase服务:

const initFirebase = () => {

    firebase.initializeApp(config.firebase)

    firebase.auth().onAuthStateChanged((res) => {
        console.log('Firebase - onAuthStateChanged', {res})

        if(res) {
            const formData = {
                email: res.email,
                uid: res.uid,
            }

            // Check if email exists against local API
            APIAuthService(endpoint, 'post', formData).then(response => {
                if(response.status) {
                    let userData = response.data
                    store.dispatch(authAction.login(userData))
                }
            })
        }
        else {
            store.dispatch(authAction.logout())
        }
    })
}

我的Login.js组件也可以在这里查看(第41行)

是的,这是正常的,onAuthStateChanged将继续触发。您可以使用以下代码防止多次调用:

var authFlag = true;
firebase.auth().onAuthStateChanged((res) => {
    if(res && authFlag) {
        console.log('Firebase - onAuthStateChanged', {res})
        authFlag = false;
        //rest of your code

我需要在我的登录组件上设置authFlag true吗?就在
const initFirebase
的内部,我有点困惑,当用户单击注销时,这仍然有效吗?我想在他们注销后将authFlag设置为true是有意义的,对吗?是的,如果你想再打一次电话,只需将authFlag设置为true:)