I need to check whenever the cookie name is Login or Default to check if the person is logged in so i wrote a simple if statement
const[cookie,setCookie,removeCookie] = useCookies(['']);
if(cookie.Login !== null){
// some code....
console.log("Login checker")
}else{
// some other code
console.log("Logout checker")
}
When i check the cookie name it is Clearly Default there is no Login Cookie (so the user is no logged in)
but it still goes throughs the first statement. and shows Login checker in the console.log
is the UseCookie just terrible or am i doing something wrong??
please help me with this
You need to provide the actual code here: what’s the function useCookie? With no idea what it is, then we can’t provide any help because there is no possible way we can know what it’s supposed to do
If you’re using the react-cookie library that turns up as the first Google result, then the first value in the array that the useCookies isn’t a cookie, it’s a object with some functions attached to it. One of those lets you pick a specific cookie (like cookies.get, as in the examples). You could have many cookies present, it can’t just be a single thing
If that isn’t the library you’re using, I’m just guessing here, then, again, you need to actually say what the useCookie function is.
Im using the “react-cookie” library
What would you recommend to check if a specific cookie exist.
the “if(cookie.Login !== null)” method is not working very well
or can i rather install a other librays or use a different method if so wich one would you recommend.
When i look at the examples. In order to get a cookie you have to type
“cookie.” and then the name of the cookie so: “cookie.Login” or “cookie.Cookiename1” etc
Sorry, it seems to have conflicting advice in the instructions. Possibly this is correct, but there is also a get method that also seems to do the same thing. What happens if you log the object?
Yes, bearing in mind what @lasjorg said, what you are now saying “as long as there isn’t a property with the key Login and the value null, run this code”. So you’re not checking that Login is there, you’re doing the almost the exact opposite. It’s running the way you by accident.
What is the value of the object you have called “cookie”. I get that cookie.Login is undefined, that is likely to mean the property Login isn’t defined, it isn’t there