Skip to content

Instantly share code, notes, and snippets.

@jason-dark
Created September 1, 2018 01:43
Show Gist options
  • Save jason-dark/6a1d934e609061b19325c7c80e4ada46 to your computer and use it in GitHub Desktop.
Save jason-dark/6a1d934e609061b19325c7c80e4ada46 to your computer and use it in GitHub Desktop.
<!-- auth state function -->
<script>
var privatePages = [
'/private'
];
var publicPages = [
'/sign-up',
'/log-in'
];
firebase.auth().onAuthStateChanged(function (user) {
var currentPath = window.location.pathname;
if (user) {
// User is signed in.
if (publicPages.includes(currentPath)) {
window.location.replace('/');
} else {
console.log('User is logged in!');
console.log('Email: ' + user.email);
console.log('UID: ' + user.uid);
signupLink.style.display = 'none';
loginLink.style.display = 'none';
loadingScreen.style.display = 'none';
}
} else {
// User is signed out.
if (privatePages.includes(currentPath)) {
window.location.replace('/');
} else {
console.log('No user is logged in');
privateLink.style.display = 'none';
logoutLink.style.display = 'none';
loadingScreen.style.display = 'none';
}
}
});
</script>
<!-- loading screen css -->
<style>
.loading-screen {
display: flex;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment