Last active
September 2, 2021 05:12
-
-
Save hieptl/a6d1a61661ba1c2c41d1b60c68860019 to your computer and use it in GitHub Desktop.
Auth.js - Javascript Chat App
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; | |
} | |
CometChatWidget.init({ | |
"appID": `${config.CometChatAppId}`, | |
"appRegion": `${config.CometChatRegion}`, | |
"authKey": `${config.CometChatAuthKey}` | |
}).then(response => { | |
CometChatWidget.CometChat.getLoggedinUser().then( | |
user => { | |
// check current page is the login page, or not. | |
const isLoginPage = window.location.href.includes('login'); | |
if(shouldRedirectToHomePage(user, isLoginPage)){ | |
window.location.href = '/'; | |
}else if(shouldRedirectToLoginPage(user, isLoginPage)) { | |
window.location.href = '/login.html'; | |
} | |
}, error => { | |
} | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment