简体   繁体   English

JS-Cookie - 无法从辅助函数设置 Cookie

[英]JS-Cookie - Can't set a Cookie from a helper function

I have a Login form where if the user is successful, we will set an authToken in the Cookie.我有一个登录表单,如果用户成功,我们将在 Cookie 中设置一个 authToken。

enter code here

 if (res.status === 202) { const result = await res.json(); console.log( "This is auth token before setting" + result.token ); //setCookie(result.token); Cookies.set("authToken", result.token, { secure: true, sameSite: "strict", expires: 1, domain: "localhost:3001", }); localStorage.setItem("username", username); console.log( "This is Cookie auth token " + Cookies.get("authToken") ); history.push("/home"); }

Now, I will use this code elsewhere so I wanted to create a helper function.现在,我将在其他地方使用此代码,因此我想创建一个辅助函数。

This doesn't work and I'm not sure why.这不起作用,我不确定为什么。

 const setCookie = (token) => { //console.log("this is from setCookie " + token); console.log( Cookies.set("authToken", token, { secure: true, sameSite: "strict", expires: 1, domain: "localhost:3001", }) ); }; export default setCookie;

However, this seems to work just fine然而,这似乎工作得很好

 import Cookies from "js-cookie"; const setCookie = (token) => { //console.log("this is from setCookie " + token); console.log( Cookies.set("authToken", token); }; export default setCookie;

I'm not sure why this is the case.我不确定为什么会这样。 Can anyone suggest why how I can fix this please?任何人都可以建议为什么我可以解决这个问题吗?

After making some changes, this seems to work.进行一些更改后,这似乎有效。

 Cookies.set("authToken", result.token, { // secure: true, sameSite: "strict", expires: 1, // domain: "localhost", });

As soon as I make the cookie secure it stops working.一旦我使 cookie 安全,它就会停止工作。

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

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