1

The title explains the whole problem, I included the OS and browser in case that makes a difference. The code is very simple:

alert(cookieString); // path=/;domain=http://localhost:3000/;expires=Fri May 01 2020 16:29:34 GMT-0400 (Eastern Daylight Time);email=test.email@emailservice.com;password=test password;
document.cookie = cookieString;
alert(document.cookie); // Empty
window.location.replace("http://localhost:3000/main.html");

I don't understand why the alert for the document.cookie is empty when the cookieString variable clearly has a valid cookie in it which I am trying to set. At first, I thought the redirect was to blame, so I then put it in a window.setTimeout, and set it to wait for 5 seconds, thinking that the browser needed time to get the cookie set. This theory did not work however, then I completely removed the redirect, and it still gave me the same output. So I then tried and experimenting with cookies in the console, when I entered the following code into the console:

document.cookie = "Test Cookie";

It worked just fine, despite not using the correct format. The fact that it works in the console and not in my code makes me think its something related to permissions, but I really don't know.

Any help would be appreciated, Thanks!

KingCoder11
  • 415
  • 4
  • 19
  • This isn't directly related to the question, but please don't store sensitive information (email, password) in a cookie! It's very insecure! – Wendelin May 01 '19 at 20:47
  • @Wendelin Im not too sure how to implement my "Remember Me" algorithm without adding cookies, if there is another way, id be pleased to consider it! – KingCoder11 May 01 '19 at 20:50
  • That question has been answered [here](https://stackoverflow.com/questions/244882/what-is-the-best-way-to-implement-remember-me-for-a-website). Basically you don't store the password in the cookie but a random string that acts as a token. The token is stored in your DB and if the token cookie matches one in your DB then you can treat them as logged in. But please read the answer to the question that I linked – Wendelin May 01 '19 at 20:56
  • Thank you! This helps a lot. I will try to implement that, however I will leave this question open as it would help me learn how to use cookies properly in the future, and others may come across the same issue in the future. – KingCoder11 May 01 '19 at 20:58

0 Answers0