1

I have a login service and account profile page and account update form page and when I login I get a token and a user back from the API backend and set the data in the store in a mutation. If I want to update a profile, I go to '/profile/edit' page and submit a form to the server and if the record gets updated on the server I get the updated user record back in the response and set this new user object in the store and redirect to user profile page at '/profile'. So far so good, I can see that the account attributes have changed on the profile page, however, when I refresh the page I get the old user object that I got after logging in so, for some reason, the new user wasn't set properly in the store and I don't understand what is going on. I would appreciate your help. Here is my code:

Store

export default new Vuex.Store({
    state: {
        auth: {
          apiToken: null,
          user: {},
        }
    },
    getters,
    mutations,
  actions
})

Login action

login({ commit }, payload){
    axios.post('/api/auth/login', payload)
        .then(response => {
            commit('authenticateUser', {
                token: response.data.api_token,
              user: response.data.user
          })
              localStorage.setItem('token', response.data.api_token)
              localStorage.setItem('user', JSON.stringify(response.data.user))

              router.push('/')
        })
        .catch(error => {
            commit('showErrorAlert')
        })
    }

AuthenticateUser mutation

authenticateUser(state, payload) {
    state.auth.apiToken = payload.token
    state.auth.user = payload.user
    state.auth.signedIn = true
  }

saveUserProfile action

saveUserProfile({ commit }, payload){
    axios.patch('/api/users/' + payload.get('id'), payload)
       .then(response => {
           commit('saveUserProfile', response.data)
           router.push('/profile')
      })
      .catch(error => {
          console.log(response)
      })
  }

saveUserProfile mutation

saveUserProfile(state, payload){
    state.auth.user = payload
    console.log(state.auth.user)
    # Here I get the updated user from the server with new attribtues
}

In userProfile component I have a computed property that fetches the user from the store.

<script>
    export default {
      computed: {
          current_user(){
             return this.$store.getters.currentUser;
             # here, after updating the account I get the new user record but when I refresh the page, I get the old record.
          }
       }
     }
</script>

When I get redirected to the profile page after updating the account I get the new attributes but after I refresh the page I get the old data. It's weird because I think VueX keeps the data in memory. I can confirm this by logging in and inspecting the state in VueX browser console. I can see that the user record is there. If I then go, for example, to '/dashboard' I can see that the user record is still there. It's pretty weird. Am I going anything wrong?

UPDATE

currentUser getter

currentUser: state => {
    return state.auth.user
}
jedi
  • 2,003
  • 5
  • 28
  • 66
  • Have you tried the following state.auth.user = Object.assign({},payload.user) – Darem Jan 09 '19 at 12:10
  • I've just tried it and same thing. – jedi Jan 09 '19 at 12:43
  • If I move between pages by clicking on a link I still have the new record. It looks like VueX does not persist the data in the store and I would have to make a new HTTP GET request to the server to fetch the user again. Still, it looks weird to me that VueX does keep the old user in the store after page reload and does not keep the new one. Or I am doing something wrong. – jedi Jan 09 '19 at 12:53
  • Do you have a SPA or MPA application? – Darem Jan 09 '19 at 13:23
  • I have several routes, like `/dashboard`, `/profile`, `/login`, `signup`, I am using VueRouter and separate components for each route accordingly. – jedi Jan 09 '19 at 13:28
  • @jedi Can you post the content of your `currentUser` getter as well. – Ana Liza Pandac Jan 09 '19 at 13:46
  • Of course, I've just added it @AnaLizaPandac – jedi Jan 09 '19 at 13:48
  • @jedi do you see in the vue devtools in the section of vuex a mutation after your http request? – Darem Jan 09 '19 at 13:53
  • @Darem Yes, I do. It's `saveUserProfile` mutation – jedi Jan 09 '19 at 13:57
  • maybe this solution could help https://stackoverflow.com/questions/43027499/vuex-state-on-page-refresh or you make a http request after the page reload. – Darem Jan 09 '19 at 14:03
  • So you are saying that VueX does not persist date on page reload? How come I still have the old user in the store, then? – jedi Jan 09 '19 at 14:08
  • When you reload the page is your Login action excecuted? – Darem Jan 09 '19 at 14:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186417/discussion-between-jedi-and-darem). – jedi Jan 09 '19 at 15:17

0 Answers0