admin管理员组文章数量:1287882
EDIT: its work / i can get the cookies value when the httpOnly = false, Why?
Im using js-cookie package,
When I open the chrome dev tools, there is a cookie token.
but when im using Cookies.get('token'), result is undefined.
and using Cookies.get() also the result is undefined.
this is my code:
import Cookies from 'js-cookie'
const [token, setToken] = useState(Cookies.get())
// const [token, setToken] = useState(Cookies.get('token'))
useEffect(() => {
console.log(token) //undefined
CheckAuth(token)
.then(data => {
setLoggedInUser(data.user)
})
.catch(error => {
Cookies.remove('token')
setErrorMessage(error.message)
})
.finally(() => {
setLoading(false)
})
}, [])
EDIT: its work / i can get the cookies value when the httpOnly = false, Why?
Im using js-cookie package,
When I open the chrome dev tools, there is a cookie token.
but when im using Cookies.get('token'), result is undefined.
and using Cookies.get() also the result is undefined.
this is my code:
import Cookies from 'js-cookie'
const [token, setToken] = useState(Cookies.get())
// const [token, setToken] = useState(Cookies.get('token'))
useEffect(() => {
console.log(token) //undefined
CheckAuth(token)
.then(data => {
setLoggedInUser(data.user)
})
.catch(error => {
Cookies.remove('token')
setErrorMessage(error.message)
})
.finally(() => {
setLoading(false)
})
}, [])
Share
Improve this question
edited Aug 31, 2021 at 1:19
Zulfikar Ahmad
asked Aug 31, 2021 at 1:15
Zulfikar AhmadZulfikar Ahmad
5043 gold badges7 silver badges21 bronze badges
4
-
Put
Cookies.get()
in a useEffect? – evolutionxbox Commented Aug 31, 2021 at 1:17 - @evolutionxbox it's the same, the result is undefined – Zulfikar Ahmad Commented Aug 31, 2021 at 1:20
- edit: i can get the result when the HttpOnly is false – Zulfikar Ahmad Commented Aug 31, 2021 at 1:21
- is that how HttpOnly works? – Zulfikar Ahmad Commented Aug 31, 2021 at 1:21
1 Answer
Reset to default 9It’s a valid behaviour. Using httpOnly = true, flag while generating a cookie, makes the cookie a protected one. And if a certain browser supports httpOnly flag, It won’t allow the client side script to use such a protected cookie. Kindly find more details here, https://owasp/www-munity/HttpOnly
本文标签: javascriptCookiesget() is undefinedStack Overflow
版权声明:本文标题:javascript - Cookies.get() is undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741323051a2372319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论