简体   繁体   English

从 NodeJS 访问 React 设置的通用 cookie

[英]Accessing universal-cookie set by React from NodeJS

My ReactJS app creates an httpOnly universal-cookie with the token after the user logs in. The code is as follows,我的 ReactJS 应用在用户登录后使用令牌创建了一个httpOnly通用 cookie。代码如下,

cookies.set('token', res.data.token, { path: '/', httpOnly: true });

In my NodeJS backend code, I am trying to access this cookie by using the following code,在我的 NodeJS 后端代码中,我尝试使用以下代码访问此 cookie,

const cookies = req.universalCookies.get('token')

When I console.log the cookies variable it shows undefined .当我console.log cookie 变量显示undefined

What am I doing wrong?我究竟做错了什么? How can I fix it?我该如何解决?

Is there any better and safe method that works?有没有更好更安全的方法? If yes then please let me know about that too.如果是,那么也请让我知道。

Node.js cannot access the cookie because it is server-side. Node.js 无法访问 cookie,因为它是服务器端的。 You can solve it by sending a token from the Header.您可以通过从 Header 发送令牌来解决它。

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

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