How to specify userId in Vue.js - Vue Cookie

659 Views Asked by At

I am using vue-cookie to set userId in cookies. On login I set a userId cookie but it does not show it in my data layer until I refresh the page. It does not even show me that the cookie is in my cookies storage.

I am using SPA so if anyone knows a work around kindly suggest as I do not want to refresh my page.

1

There are 1 best solutions below

3
Syed Ameer Hamza On

Inside your login API call please do the following, first, install package from https://www.npmjs.com/package/vue-cookies. after that when you receive user object from API call you can do the following.

   axios.get("{yourEndPoint}",      
          {
              headers:
          {
            'Content-Type': 'multipart/form-data',
            'Authorization': 'Bearer ' + token
          }
          })
          .then(resp => {
           this.$store.commit(auth/setUserID, resp);
          }).

Inside your store You can do following

    import VueCookies from 'vue-cookies';

    //Mutation to set user ID in cookie
    setUserID: function (state, userID) {
    console.log('Storing User ID...');
    VueCookies.set(user_id, userID);
    }