首页 文章

Vue Firebase:正在创建但在其他用户帐户下登录的用户

提问于
浏览
4

我在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');
        }
    },

所以基本上我正在做以下事情

  • 使用Firebase创建新用户 .

  • 将该用户保留到Firebase数据库 .

  • 根据我从Firebase获得的一些数据更新Vuex状态 .

当我刚用新密码创建一个新用户时,没有任何反应,我在控制台中得到以下内容 .

enter image description here

但是,当我使用之前创建的密码创建另一个新用户时,它会在最后一个用户(使用新密码创建的用户)的信息下登录 . 但是,使用新密码创建的用户不会存储在Firebase中 .

这真的是我遇到的一个古怪的问题,我真的不确定是什么导致它 . uid错误是我在控制台中遇到的唯一错误 . 在创建用户时我可以不使用承诺吗?我添加了这个,因为用户没有被添加到firebase,因为我在创建帐户后无法抓取用户 .

真的可以在这一个上使用另一双眼睛 .

UPDATE: 当我尝试使用我的信息登录时,我的信息似乎被另一个用户信息覆盖在firebase中 . 然后,我还会在应用程序中显示所有用户信息 . 然后每当我尝试使用我的电子邮件/密码登录时,我都会以John Steve的身份登录 .

enter image description here

谢谢!

1 回答

  • 1

    错误 Cannot read property 'uid' of null 是因为firebase在初始化时可以为当前用户返回null请参阅manage-users guide

    第二个问题我认为这是因为你在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
             }).then(()=>{
                // Push to home after user data has been saved
                router.push({ path: '/' });
            });
          });  
        });
        }
      }
    }
    

相关问题