Last active
September 2, 2021 05:17
-
-
Save hieptl/b4cbed717d430d6f03ecc02ddb9775d4 to your computer and use it in GitHub Desktop.
Login.js - Creating a New Account - 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
... | |
/** | |
* register a new account - register with firebase and cometchat. | |
* @param {*} object - user's information that will be used to register. | |
*/ | |
function registerNewAccount({email, password, confirmPassword}) { | |
if (validateNewAccount({email, password, confirmPassword})) { | |
// show loading indicator. | |
showLoading(); | |
// get user avatar. | |
const userAvatar = generateAvatar(); | |
// create new user's uuid. | |
const userUuid = uuid.v4(); | |
// call firebase and cometchat service to register a new account. | |
auth.createUserWithEmailAndPassword(email, password).then((userCrendentials) => { | |
if (userCrendentials) { | |
// call firebase real time database to insert a new user. | |
realTimeDb.ref(`users/${userUuid}`).set({ | |
id: userUuid, | |
email, | |
avatar: userAvatar | |
}).then(() => { | |
alert(`${userCrendentials.user.email} was created successfully! Please sign in with your created account`); | |
// call cometchat service to register a new account. | |
const user = new CometChatWidget.CometChat.User(userUuid); | |
user.setName(email); | |
user.setAvatar(userAvatar); | |
CometChatWidget.init({ | |
"appID": `${config.CometChatAppId}`, | |
"appRegion": `${config.CometChatRegion}`, | |
"authKey": `${config.CometChatAuthKey}` | |
}).then(response => { | |
CometChatWidget.createOrUpdateUser(user).then(user => { | |
hideLoading(); | |
} ,error => { | |
hideLoading(); | |
}); | |
hideSignUp(); | |
}, error => { | |
//Check the reason for error and take appropriate action. | |
}); | |
}); | |
} | |
}).catch((error) => { | |
hideLoading(); | |
alert(`Cannot create your account, ${email} might be existed, please try again!`); | |
}); | |
} | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment