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
Add a ment  | 

1 Answer 1

Reset to default 9

It’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