Last active
September 12, 2021 07:27
-
-
Save hieptl/fb516126783754045eb699b4c99fdeff to your computer and use it in GitHub Desktop.
SignUp.js - Mention 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
// import useRef, useContext | |
import { useRef, useContext } from "react"; | |
// import Context to get shared data. | |
import Context from "../Context"; | |
// import validator to validate user's information. | |
import validator from "validator"; | |
// import firebase authentication. | |
import { auth, realTimeDb } from "../firebase"; | |
// import uuid to generate id for users. | |
import { v4 as uuidv4 } from "uuid"; | |
function SignUp(props) { | |
// get toggleModal functin from higher order components. | |
const { toggleModal } = props; | |
// create refs to get user's email, user's password, user's confirm password. | |
const emailRef = useRef(null); | |
const userNameRef = useRef(null); | |
const passwordRef = useRef(null); | |
const confirmPasswordRef = useRef(null); | |
const { cometChat, setIsLoading } = useContext(Context); | |
/** | |
* generate random avatar for demo purpose | |
* @returns | |
*/ | |
const generateAvatar = () => { | |
// hardcode list of user's avatars for the demo purpose. | |
const avatars= [ | |
'https://data-us.cometchat.io/assets/images/avatars/captainamerica.png', | |
'https://data-us.cometchat.io/assets/images/avatars/cyclops.png', | |
'https://data-us.cometchat.io/assets/images/avatars/ironman.png', | |
'https://data-us.cometchat.io/assets/images/avatars/spiderman.png', | |
'https://data-us.cometchat.io/assets/images/avatars/wolverine.png' | |
]; | |
const avatarPosition = Math.floor(Math.random() * avatars.length); | |
return avatars[avatarPosition]; | |
} | |
/** | |
* validate user's informatin. | |
* @param {*} param0 | |
* @returns | |
*/ | |
const isSignupValid = ({ email, password, userName, confirmPassword }) => { | |
if (!validator.isEmail(email)) { | |
alert("Please input your email"); | |
return false; | |
} | |
if (validator.isEmpty(userName) || userName.includes(' ')) { | |
alert("Please input your user name. Your user name must not contain white space"); | |
return false; | |
} | |
if (validator.isEmpty(password) || !validator.isLength(password, {min: 6})) { | |
alert("Please input your password. You password must have at least 6 characters"); | |
return false; | |
} | |
if (validator.isEmpty(confirmPassword)) { | |
alert("Please input your confirm password"); | |
return false; | |
} | |
if (password !== confirmPassword) { | |
alert("Confirm password and password must be the same"); | |
return false; | |
} | |
return true; | |
}; | |
/** | |
* sign up | |
*/ | |
const signup = () => { | |
// get user's email, user's password, user's confirm password. | |
const email = emailRef.current.value; | |
const userName = userNameRef.current.value; | |
const password = passwordRef.current.value; | |
const confirmPassword = confirmPasswordRef.current.value; | |
if (isSignupValid({ email, password, userName, confirmPassword })) { | |
// show loading | |
setIsLoading(true); | |
// create new user's uuid. | |
const userUuid = uuidv4(); | |
// generate user's avatar. | |
const userAvatar = generateAvatar(); | |
// call firebase to 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, | |
userName, | |
avatar: userAvatar | |
}).then(() => { | |
alert(`${userCrendentials.user.email} was created successfully! Please sign in with your created account`); | |
// cometchat auth key | |
const authKey = `${process.env.REACT_APP_COMETCHAT_AUTH_KEY}`; | |
// call cometchat service to register a new account. | |
const user = new cometChat.User(userUuid); | |
user.setName(userName); | |
user.setAvatar(userAvatar); | |
cometChat.createUser(user, authKey).then( | |
user => { | |
setIsLoading(false); | |
},error => { | |
setIsLoading(false); | |
} | |
) | |
// close sign up dialog. | |
toggleModal(false); | |
}); | |
} | |
}).catch((error) => { | |
console.log(error); | |
setIsLoading(false); | |
alert(`Cannot create your account, ${email} might be existed, please try again!`); | |
}); | |
} | |
}; | |
return ( | |
<div className="signup"> | |
<div className="signup__content"> | |
<div className="signup__container"> | |
<div className="signup__title">Sign Up</div> | |
<div className="signup__close"> | |
<img | |
alt="close" | |
onClick={() => toggleModal(false)} | |
src="https://static.xx.fbcdn.net/rsrc.php/v3/y2/r/__geKiQnSG-.png" | |
/> | |
</div> | |
</div> | |
<div className="signup__subtitle"></div> | |
<div className="signup__form"> | |
<input type="text" placeholder="Email" ref={emailRef} /> | |
<input type="text" placeholder="Username" ref={userNameRef} /> | |
<input type="password" placeholder="Password" ref={passwordRef} /> | |
<input | |
type="password" | |
placeholder="Confirm Password" | |
ref={confirmPasswordRef} | |
/> | |
<button className="signup__btn" onClick={signup}> | |
Sign Up | |
</button> | |
</div> | |
</div> | |
</div> | |
); | |
} | |
export default SignUp; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment