简体   繁体   English

Vue-Cookies 无法获取 Cookie

[英]Vue-Cookies Can't Get Cookie

I am trying to setup an OAuth service from my Node.js back-end to my Vue front-end.我正在尝试从我的 Node.js 后端到我的 Vue 前端设置 OAuth 服务。 Currently, the back-end appears to be working correctly;目前,后端似乎工作正常; I am able to authenticate successfully with my google account and then am redirected to the appropriate UI View upon authentication.我能够使用我的 google 帐户成功进行身份验证,然后在身份验证后重定向到相应的 UI 视图。 The cookie is also being stored. cookie 也正在存储。

开发工具中的 cookie

  • My node service is running on localhost:3000我的节点服务在 localhost:3000 上运行
  • My Vue UI is running on localhost:8080我的 Vue UI 在 localhost:8080 上运行

I can access the cookie using document.cookie , and it shows:我可以使用document.cookie访问 cookie,它显示:

"express:sess=<Data here>; express:sess.sig=<Data here>"

The problem is that I am using vue-cookies , and can't retrieve the cookie using window.$cookies.get(...);问题是我正在使用vue-cookies ,并且无法使用window.$cookies.get(...);检索cookie window.$cookies.get(...);

In my main.js file I am importing vue-cookies:在我的main.js文件中,我正在导入 vue-cookies:

import VueCookies from "vue-cookies";
Vue.use(VueCookies);
window.$cookies.config("7D", "", "localhost");

new Vue({
  router,
  store,
  vuetify,
  render: h => h(App)
}).$mount("#app");

Here is my router/index.js where I am using that code:这是我使用该代码的router/index.js

import Vue from "vue";

...
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    debugger;
    if (!window.$cookies) {
      next({
        path: "/login",
        params: { nextUrl: to.fullPath }
      });
    } else {
      const cookie = window.$cookies;
      if (to.matched.some(record => record.meta.isAdmin)) {
        if (cookie.isAdmin) {
          next();
        } else {
          next({
            path: "/login",
            params: { nextUrl: to.fullPath }
          });
        }
      } else {
        next();
      }
    }
  } else {
    next();
  }
});

If I try to log window.$cookies.get("express:sess") , it returns null .如果我尝试记录window.$cookies.get("express:sess") ,它会返回null Not sure what I am doing wrong.不知道我做错了什么。 I know that window.$cookies is defined from the console as well, because I can see all the methods/properties on that object.我知道window.$cookies也是从控制台定义的,因为我可以看到该对象上的所有方法/属性。

If I create a cookie from the developer tools, with a name of "test", I can retrieve it:如果我从开发人员工具创建一个名为“test”的 cookie,我可以检索它:

> window.$cookies.get("test")
> "value"

EDIT :编辑

If I change the cookie that I manually create to have a name of test:test , the output is null!如果我将手动创建的 cookie 更改为名称test:test ,则输出为空! Does the : character have something to do with why I can't retrieve the cookie?? :字符是否与我无法检索 cookie 的原因有关?

> window.$cookies.get("test:test")
> null

EDIT 2: I can do the following and see the cookies are there, but cannot access them for some reason.编辑 2:我可以执行以下操作并查看 cookie 是否在那里,但由于某种原因无法访问它们。

window.$cookies.keys()
(3) […]
​
0: "express:sess"
​
1: "express:sess.sig"
​
2: "test"
​
length: 3

Your problem relies on how vue-cookie manages cookies.您的问题取决于 vue-cookie 如何管理 cookie。 if you take a look a the set method you can see that vue-cookie encodes your values before storing the cookie.如果您查看set 方法,您可以看到 vue-cookie 在存储 cookie 之前对您的值进行编码。

Example:例子:

let's say you store a cookie using vue-cookie:假设您使用 vue-cookie 存储 cookie:

$cookies.set('express:sess')

this will store the name of the cookie as express%3Asess which is the result of encodeURIComponent('express:sess')这会将 cookie 的名称存储为express%3Asess ,这是encodeURIComponent('express:sess')

I recommend using a different library or just using native browser API's我建议使用不同的库或仅使用本机浏览器 API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM