admin管理员组文章数量:1317915
import Cookies from 'js-cookie';
//this returns null
const authTokenFromCookies = Cookies.get('authToken');
console.log("AuthToken from cookies:", authTokenFromCookies);
// This returns the token set from the backend
const authToken = response.headers['authToken'];
console.log("AuthToken after login:", authToken);
js-cookie returns null but headers return the set cookies, Why?
What could be the cause?
import Cookies from 'js-cookie';
//this returns null
const authTokenFromCookies = Cookies.get('authToken');
console.log("AuthToken from cookies:", authTokenFromCookies);
// This returns the token set from the backend
const authToken = response.headers['authToken'];
console.log("AuthToken after login:", authToken);
js-cookie returns null but headers return the set cookies, Why?
What could be the cause?
2 Answers
Reset to default 1js-cookie returns null but headers return the set cookies, Why?
Because a cookie named authToken
, and a response header named authToken
, are absolutely not the same thing.
A cookie would be set via a Set-Cookie
header. (A response header Set-Cookie: authToken=value
would create a cookie named authToken
.)
You did not log any actual cookie there in that second block, you logged the value of the header named authToken
.
Hope this answers your query.
It should be noted that js-cookie
can only interact with cookies which are accessible by javascript.
As @c3roe rightly mentioned, you are receiving null
because there is no javascript-accessible cookie with the name authToken
.
Debug Approach:
- Check if there is a cookie with name
authToken
via devTools. - If the cookie is available is it a
httpOnly
cookie ( httpOnly cookies are inaccessible by javascript ).
本文标签: javascriptjscookie returns null but headers return the set cookieswhyStack Overflow
版权声明:本文标题:javascript - js-cookie returns null but headers return the set cookies, Why? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737379421a1986593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论