Skip to content

Instantly share code, notes, and snippets.

@swahaniroy
Created May 26, 2022 15:07
Show Gist options
  • Save swahaniroy/43c33d391e7aec9e744e4119355699ad to your computer and use it in GitHub Desktop.
Save swahaniroy/43c33d391e7aec9e744e4119355699ad to your computer and use it in GitHub Desktop.
The Auth File
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