Created
May 26, 2022 15:11
-
-
Save swahaniroy/c75b6e1190667f2da85cb11cad364831 to your computer and use it in GitHub Desktop.
the login.js file
This file contains 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) => { | |
console.log(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