I have a temporary login script, that checks a password, assigns a cookie, and then redirects the user to a new page. On the console page, I have code that checks if the cookie is set to true
. If it is, it deletes it and continues. If it isn't, it redirects the user back to the homepage. I got all the code in place, but it doesn't work! Here's some more important info:
- The cookie is called
login
- the logged in cookie is set to
true
- The cookie sets properly, it's a problem with the console page's code. Here's my code:
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}
function doSomething() {
var myCookie = getCookie("login");
if (myCookie == true) {
alert("Logged in for one session!");
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}
else {
alert("Login Failed. Redirecting...");
window.location.assign("https://www.code-u.org/")
}
}