Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access vue-cookies in a vuex store?

In an action, I'm doing:

this.$cookies.set("token", response.token, "7d");
this.$cookies.set("UserId", response.UserId, "7d");

But alas $cookies is not defined.

like image 540
Shamoon Avatar asked Aug 19 '19 14:08

Shamoon


3 Answers

You can install js-cookie library and than access cookie like this in vuex store:

import Cookies from 'js-cookie'

const getters = {
  isLoggedIn: function (state) {
    return !!state.email && !!Cookies.get('access_token')
  }
}
like image 56
Diego Armando Maradona Avatar answered Oct 18 '22 01:10

Diego Armando Maradona


you can do (in store.js)

import Vue from 'vue'

...

someAction () {
    Vue.prototype.$cookies.set("token", response.token, "7d");
}
like image 43
Nafees Anwar Avatar answered Oct 18 '22 01:10

Nafees Anwar


When using vue-cookies in your app

In your store.js, you can

import cookie from 'vue-cookies'

.....

action(){
   cookie.get('token');
}
like image 7
notrexbias Avatar answered Oct 18 '22 03:10

notrexbias