Created
September 28, 2021 09:13
-
-
Save hieptl/6ae3f97dd0971bd9071b5f2139bfc62e to your computer and use it in GitHub Desktop.
auth.js - Client Side - Tinder Clone
This file contains hidden or 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
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