How to get cookie in Vue Router in Vue 2?

457 Views Asked by At

I want to manage cookies in the browser and use this external plugin vue-cookies. But if I call its window.$cookies.isKey(Cookies), I get an error: console.log(currentUser) = false. Cookie exists on browser

Main.js:

import Vue from "vue"
import VueCookies from 'vue-cookies'
import App from "./App.vue"
Vue.use(VueCookies)
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");

routes.js:

router.beforeEach((to, from, next) => {
 
  const currentUser = window.$cookies.isKey(Cookies);

  console.log(currentUser);

  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (!currentUser) {
      window.location.href = "https://localhost";
    } else {
      next();
    } {
      next();
    }
  } 
});

Any idea?

0

There are 0 best solutions below