Created
September 29, 2021 08:52
-
-
Save khooz/6364414ea4a5145eab431b907b4bf4af to your computer and use it in GitHub Desktop.
Simple age restriction guard that only relies on js-cookie.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure js-cookies are loaded before this code | |
function verifyAge(gaurdingPage = "./age-verification.html") { | |
if (!Cookies.get("age-verification")) | |
{ | |
Cookies.set("intended", window.location.href); | |
window.location.replace(gaurdingPage); | |
} | |
} | |
function checkAge(homePage = "./index.html", awayPage = "https://www.google.com/?q=") { | |
function goToIntendedOrDefault(defaultAddress = "./index.html") { | |
var current_intent = Cookies.get("intended"); | |
if (!current_intent) | |
{ | |
window.location.replace(defaultAddress); | |
} | |
else | |
{ | |
Cookies.remove("intended"); | |
window.location.replace(current_intent); | |
} | |
} | |
if (Cookies.get("age-verification")) | |
{ | |
goToIntendedOrDefault(homePage); | |
} | |
document.getElementById("enter").addEventListener("click", function(event) | |
{ | |
Cookies.set("age-verification", true, {expires: 30}); | |
goToIntendedOrDefault(); | |
}); | |
document.getElementById("leave").addEventListener("click", function(event) | |
{ | |
Cookies.set("age-verification", false, {expires: 30}); | |
window.location.assign(awayPage) | |
}); | |
} | |
/** | |
Usage: | |
In every page, simply use verifyAge with the guard page address. | |
In the guard page, make buttons with id="enter" for verified and id="leave" for not verified and call checkAge with your home page address, and an address to redirect people when they are not verified. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment