Created
September 28, 2021 11:51
-
-
Save hieptl/87c88870b656f358dbc9a685bd897d86 to your computer and use it in GitHub Desktop.
login.js - Login Client Side - Tinder Clone
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 })) { | |
axios | |
.post("/login", { email, password }) | |
.then((res) => { | |
if (res && res.data && res.data.uid) { | |
const appSetting = new CometChat.AppSettingsBuilder() | |
.subscribePresenceForAllUsers() | |
.setRegion(config.CometChatRegion) | |
.build(); | |
CometChat.init(config.CometChatAppId, appSetting).then( | |
() => { | |
// You can now call login function. | |
CometChat.login(res.data.uid, config.CometChatAuthKey).then( | |
(loggedInUser) => { | |
// hide loading. | |
hideLoading(); | |
// store logged in user in the local storage. | |
localStorage.setItem("auth", JSON.stringify({ ...loggedInUser, gender: res.data.gender })); | |
// redirect to home page. | |
window.location.href = "/"; | |
} | |
); | |
}, | |
(error) => { | |
// Check the reason for error and take appropriate action. | |
} | |
); | |
} else { | |
// hide loading. | |
hideLoading(); | |
alert("Your user name or password is not correct"); | |
} | |
}) | |
.catch((error) => { | |
if (error) { | |
hideLoading(); | |
alert("Your user 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