Vue + Firebase: users are created, but logged in under different user accounts

I have a rather strange problem in Firebase where, if I create a new user with the same password as the previous user, that will be created by a new user with the correct information, but will be registered in the previous user account and will display all previous user data.

I log out, and then I log in with the new user information, and only after that I returned the correct information. I also had a problem with a new user rewriting the previous user path in Firebase if I used the same password, but I cannot recreate it.

My code for creating a new user is as follows

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 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.
  • Vuex , Firebase.

, , .

enter image description here

, , , (, ). , , Firebase.

bizzare, , , . uid - , . ? , firebase, .

.

: , firebase . . , /, .

enter image description here

!

+4
1

Cannot read property 'uid' of null , firebase null , .

, , - , , 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: '/' });
        });
      });  
    });
    }
  }
} 
+1

Source: https://habr.com/ru/post/1666280/


All Articles