Skip to content

Instantly share code, notes, and snippets.

@hieptl
Last active September 2, 2021 05:12
Show Gist options
  • Save hieptl/a6d1a61661ba1c2c41d1b60c68860019 to your computer and use it in GitHub Desktop.
Save hieptl/a6d1a61661ba1c2c41d1b60c68860019 to your computer and use it in GitHub Desktop.
Auth.js - Javascript Chat App
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