Skip to content

Instantly share code, notes, and snippets.

@hieptl
Created September 28, 2021 09:13
Show Gist options
  • Save hieptl/6ae3f97dd0971bd9071b5f2139bfc62e to your computer and use it in GitHub Desktop.
Save hieptl/6ae3f97dd0971bd9071b5f2139bfc62e to your computer and use it in GitHub Desktop.
auth.js - Client Side - Tinder Clone
window.addEventListener("DOMContentLoaded", function () {
function shouldRedirectToHomePage(user, isLoginPage) {
return user && isLoginPage;
}
function shouldRedirectToLoginPage(user, isLoginPage) {
return !user && !isLoginPage;
}
const authenticatedUser = JSON.parse(localStorage.getItem("auth"));
// check current page is the login page, or not.
const isLoginPage = window.location.href.includes("login");
if (shouldRedirectToHomePage(authenticatedUser, isLoginPage)) {
window.location.href = "/";
} else if (shouldRedirectToLoginPage(authenticatedUser, isLoginPage)) {
window.location.href = "/login.html";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment