2017-01-09 31 views
4

我在Firebase中遇到了一個非常奇怪的問題,如果我使用與之前用戶相同的密碼創建新用戶,正確的信息,但正在以先前的用戶帳戶登錄並顯示所有用戶以前的信息。Vue + Firebase:正在創建用戶但在其他用戶帳戶下登錄的用戶

我註銷,然後我登錄新的用戶信息,然後才顯示正確的信息。如果我使用相同的密碼,我還有一個新用戶覆蓋Firebase中以前的用戶路徑問題,但似乎無法重新創建。

我的代碼來創建一個新的用戶如下

createNewUser() { 

    // Create a new user with email and password. 

    var email = this.newUserEmail; 
    var password = this.newUserPassword; 

    firebaseAuth.createUserWithEmailAndPassword(email, password).catch(function(error) { 
     // Handle Errors here. 
     var errorCode = error.code; 
     var errorMessage = error.message; 
     // ... 
    }).then(() => { 

     firebaseAuth.onAuthStateChanged(user => { 
     database.ref('/users/' + user.uid).set({ 
      name: this.newUserName, 
      email: this.newUserEmail, 
      isRetailer: false, 
      isAdmin: false 
     }); 
     }); 
    }); 

    // Push to home 

    router.push({ path: '/' }); 

    } 
    } 
} 

然後將新創建的用戶取到下面的代碼在created() {}方法運行

// Checks for a user and dispatches an action changing isAuthed state to true. 
firebaseAuth.onAuthStateChanged(user => { 
    console.log(store.state.authentication); 
    console.log(user); 
    store.dispatch('checkUser', user); 
}) 

最後的主網頁,這發出一個動作,其運行如下

checkUser (context, user) { 

    if (user.uid) { 

    context.commit('authUser', user); 

    // checkUser runs once when the app is initialized. 

    // Gets value of the user's isRetailer property from firebase. 

    firebase.database() 
     .ref('/users/' + user.uid + '/isRetailer/') 
     .once('value').then(snapshot => context.commit('isRetailer', { 
      value: snapshot.val() 
     })); 

     // Gets name of the current user from Firebase and will eventually store that in Vuex state. 

     firebase.database() 
      .ref('/users/' + user.uid + '/name/') 
      .once('value').then(snapshot => context.commit('getDisplayName', { 
       name: snapshot.val() 
      })); 
     } else { 
      console.log('No user currently logged in'); 
     } 
    }, 

所以基本上我在做以下工作

  1. 使用Firebase創建一個新用戶。
  2. 將該用戶保存到Firebase數據庫。
  3. 根據我從Firebase獲得的一些數據更新Vuex狀態。

當我剛剛創建一個全新的密碼的新用戶,沒有任何反應,我在控制檯中得到以下內容。

enter image description here

然而,當我創建一個使用先前已創建密碼的另一個新用戶,它記錄了我在,但最後的用戶(即用新的密碼創建的)的信息下。但是,使用新密碼創建的用戶不會存儲在Firebase中。

這真的是一個bizzare問題,我真的不知道是什麼原因造成的。 uid錯誤是我在控制檯中遇到的唯一錯誤。創建用戶時,我不能使用承諾嗎?我之所以加入這個功能是因爲用戶並沒有被添加到Firebase中,因爲在帳戶創建後我無法抓住用戶。

真的可以使用另一組眼睛在這一個。

更新:當試圖用我的信息登錄時,似乎我的信息被firebase中的另一個用戶信息覆蓋。然後,我也會在應用程序中顯示所有用戶信息。然後每當我嘗試用我的電子郵件/密碼登錄時,我從現在開始以約翰史蒂夫的身份登錄。

enter image description here

謝謝!

回答

1

錯誤Cannot read property 'uid' of null是因爲火力可以爲當前用戶返回null在初始化,請參閱manage-users guide

而第二個問題,我認爲這是由於火力點已經完成,以節省用戶之前你了變化路徑的家(記該任務是異步的)

createNewUser() { 

    // Create a new user with email and password. 

    var email = this.newUserEmail; 
    var password = this.newUserPassword; 

    firebaseAuth.createUserWithEmailAndPassword(email, password).catch(function(error) { 
     // Handle Errors here. 
     var errorCode = error.code; 
     var errorMessage = error.message; 
     // ... 
    }).then(() => { 

     firebaseAuth.onAuthStateChanged(user => { 
     database.ref('/users/' + user.uid).set({ 
      name: this.newUserName, 
      email: this.newUserEmail, 
      isRetailer: false, 
      isAdmin: false 
     }).then(()=>{ 
      // Push to home after user data has been saved 
      router.push({ path: '/' }); 
     }); 
     }); 
    }); 
    } 
    } 
} 
+0

好的。我已經在第一個回調中刪除了'firebaseAuth.onAuthStateChanged(user => {})'並將其替換爲'var user = firebaseAuth.currentUser;'。然後使用'router.push()'方法刪除第二個回調,並將'router.push()'移到回調函數之外。這似乎是擺脫我的錯誤。我會在這裏做更多的測試並看看。非常感謝!! – maxwellgover

+1

現在我認爲這解決了我的問題。希望它有!謝謝你的領導:) – maxwellgover