1

I use firebase to store my users and their personal data.

I think there is no link between the information of the users and the authentication of my users. After registration the user profile displays all the information. But when I connect, I can only display the authentication information

How can I associate the two ?

register.vue :

register (){
   if (this.isFormValid()){
      firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then( user => {
         user.updateProfile({
            displayName: this.firstname,
            }).then( ()=>{
               this.saveUserToUsersRef(user).then( ()=>{
                  const user = {
                    email: this.email,
                    firstname: this.firstname,
                    lastname: this.lastname,
                    manager: this.manager,
                    entreprise: this.entreprise,
                  }

          // this.writeUserData(firebase.uid, firebase.email)

            //execute la fonction setUser dans le fichier store (Vuex)
            this.$store.dispatch("setUser", user)
            //change de page
            this.$router.push('./profile')

          })
        }, error => {
          this.errors.push(error.message)
        })

      }).catch( error => {
        this.errors.push(error.message)
      })
    }

  },
  saveUserToUsersRef(user){
    return this.usersRef.child(user.uid).set({
      email: user.email,
      firstname: user.displayName,
      lastname: this.lastname,
      manager: this.manager,
      entreprise: this.entreprise,
    })
  },

store.js :

const mutations = {
  SET_USER(state, user){
    state.currentUser = user
  }
}

const actions = {
  setUser({commit},user){
    commit("SET_USER",user)
  },
  createUser({commit},user){
    firebase.database().ref('users').push(user)
      .then((user) => {
        commit('createUser', user)
      }).catch( (error) => {
        console.log(error)
    })
  }
}

const getters = {
  currentUser: state => state.currentUser
}

login.vue :

export default {
    name: 'Profile',
    computed: {
      ...mapGetters(['currentUser'])
    }
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
DenisMasot
  • 697
  • 5
  • 11
  • 21
  • Instead of `push()` you should use something like `firebase.database().ref('users').child(user.uid).set(user)`. Also take a look at [the docs](https://firebase.google.com/docs/database/web/read-and-write#basic_write) – André Kool Mar 28 '18 at 12:15
  • I did it in the function saveUserToUsersRef. Thank you for your reply – DenisMasot Mar 28 '18 at 12:21
  • You can also take a look at [this answer i wrote](https://stackoverflow.com/a/30912711/4916627) about linking users to their data. – André Kool Mar 28 '18 at 12:25
  • 1
    Possible duplicate of [How do I link each user to their data in Firebase?](https://stackoverflow.com/questions/30910704/how-do-i-link-each-user-to-their-data-in-firebase) – André Kool Mar 28 '18 at 13:11
  • Yes that's it but I can not integrate it in Vuejs – DenisMasot Mar 29 '18 at 06:43

0 Answers0