Last active
September 2, 2021 06:54
-
-
Save hieptl/b4d60412cc6e0f251ba9ad810a739396 to your computer and use it in GitHub Desktop.
Login - 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
... | |
loginBtn.addEventListener('click', function() { | |
// show loading indicator. | |
showLoading(); | |
// get input user's credentials. | |
const email = emailLoginInputElement ? emailLoginInputElement.value : null; | |
const password = passwordLoginInputElement ? passwordLoginInputElement.value : null; | |
if(isUserCredentialsValid({email, password})) { | |
// if the user's credentials are valid, call Firebase authentication service. | |
auth.signInWithEmailAndPassword(email, password).then((userCredential) => { | |
const userEmail = userCredential.user.email; | |
realTimeDb.ref().child('users').orderByChild('email').equalTo(userEmail).on("value", function(snapshot) { | |
const val = snapshot.val(); | |
if (val) { | |
const keys = Object.keys(val); | |
const user = val[keys[0]]; | |
if (user && user.id) { | |
CometChatWidget.init({ | |
"appID": `${config.CometChatAppId}`, | |
"appRegion": `${config.CometChatRegion}`, | |
"authKey": `${config.CometChatAuthKey}` | |
}).then(response => { | |
CometChatWidget.login({uid: user.id}).then((loggedInUser) => { | |
// User loged in successfully. | |
// hide loading. | |
hideLoading(); | |
// redirect to home page. | |
window.location.href = '/'; | |
}); | |
}, error => { | |
//Check the reason for error and take appropriate action. | |
}); | |
} else { | |
// hide loading indicator. | |
hideLoading(); | |
alert(`Your user's name or password is not correct`); | |
} | |
} | |
}); | |
}) | |
.catch((error) => { | |
// hide loading indicator. | |
hideLoading(); | |
alert(`Your user's name or password is not correct`); | |
}); | |
} else { | |
// hide loading indicator. | |
hideLoading(); | |
alert(`Your user's name or password is not correct`); | |
} | |
}); | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment