From e7d09002a7316b3d75854b04354e8dd20c13dd2f Mon Sep 17 00:00:00 2001 From: Taliayaya Date: Tue, 15 Mar 2022 22:45:02 +0100 Subject: [PATCH] Create Account Fix --- src/pages/Login/index.jsx | 62 +++++++++++++++++++++----------------- src/pages/Signup/index.jsx | 47 ++++++++++++++--------------- src/utils/function.js | 9 ++++-- 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/pages/Login/index.jsx b/src/pages/Login/index.jsx index a23e22e..bd1e13e 100755 --- a/src/pages/Login/index.jsx +++ b/src/pages/Login/index.jsx @@ -42,7 +42,7 @@ function Login() { const { state } = useLocation() const [nameEmail, setnameEmail] = useState('') const [password, setpassword] = useState('') - const [error, seterror] = useState(null) + const [error, setError] = useState(null) const [showPassword, setShowPassword] = useState(false) const [stayLogged, setStayLogged] = useState(false) const auth = getAuth() @@ -53,24 +53,31 @@ function Login() { const googleSignInApi = async () => { signInWithPopup(auth, provider) .then(async (result) => { - await updateDoc(doc(db, 'users', result.user.uid), { - 'data.lastLogin': Timestamp.fromDate(new Date()), - }) + try { + await updateDoc(doc(db, 'users', result.user.uid), { + 'data.lastLogin': Timestamp.fromDate(new Date()), + }) - login().then(() => { - navigate(state?.path || '/app') - }) + login().then(() => { + navigate(state?.path || '/app') + }) + } catch { + setError( + "Il n'existe pas de compte existant avec cette adresse mail." + ) + } }) .catch((error) => { - // Handle Errors here. - const errorCode = error.code - const errorMessage = error.message - // The email of the user's account used. - const email = error.email - // The AuthCredential type that was used. - const credential = GoogleAuthProvider.credentialFromError(error) - // ... - console.log(errorCode, errorMessage, email, credential) + // // Handle Errors here. + // const errorCode = error.code + // const errorMessage = error.message + // // The email of the user's account used. + // const email = error.email + // // The AuthCredential type that was used. + // const credential = GoogleAuthProvider.credentialFromError(error) + // // ... + // // console.log(errorCode, errorMessage, email, credential) + setError('Il y a eu une erreur') }) } async function handleLogin(e) { @@ -79,10 +86,16 @@ function Login() { signInWithEmailAndPassword(auth, nameEmail, password) .then(async (userCredential) => { - await updateDoc(doc(db, 'users', userCredential.user.uid), { - 'data.lastLogin': Timestamp.fromDate(new Date()), - 'data.email': userCredential.user.email, - }) + try { + await updateDoc(doc(db, 'users', userCredential.user.uid), { + 'data.lastLogin': Timestamp.fromDate(new Date()), + 'data.email': userCredential.user.email, + }) + } catch { + setError( + "Il n'existe pas de compte existant avec cette adresse mail." + ) + } // Signed in login().then(() => { @@ -90,8 +103,7 @@ function Login() { }) }) .catch((error) => { - const errorCode = error.code - seterror(errorCode) + setError(error.message) }) } useEffect(() => { @@ -117,11 +129,7 @@ function Login() { Connexion - {error && ( - - Votre identifiant ou mot de passe est incorrect - - )} + {error && {error}} { - signInWithPopup(auth, provider) - .then(async (result) => { - const data = { - lastLogin: Timestamp.fromDate(new Date()), - uid: result.user.uid, - name: result.user.displayName, - avatar: result.user.photoURL, - } - await setDoc(doc(db, 'users', result.user.uid), { - data, - }) - await sendEmailVerification(result.user) - navigate('/join') - }) - .catch((error) => { - // Handle Errors here. - const errorCode = error.code - const errorMessage = error.message - // The email of the user's account used. - const email = error.email - // The AuthCredential type that was used. - const credential = GoogleAuthProvider.credentialFromError(error) - // ... - console.log(errorCode, errorMessage, email, credential) + signInWithPopup(auth, provider).then(async (result) => { + const data = { + lastLogin: Timestamp.fromDate(new Date()), + uid: result.user.uid, + name: result.user.displayName, + avatar: result.user.photoURL, + } + await setDoc(doc(db, 'users', result.user.uid), { + data, }) + await sendEmailVerification(result.user) + navigate('/join') + }) + // .catch((error) => { + // // Handle Errors here. + // const errorCode = error.code + // const errorMessage = error.message + // // The email of the user's account used. + // const email = error.email + // // The AuthCredential type that was used. + // const credential = GoogleAuthProvider.credentialFromError(error) + // // ... + // // console.log(errorCode, errorMessage, email, credential) + // }) } async function handleSignIn(e) { diff --git a/src/utils/function.js b/src/utils/function.js index ef2b767..aed9222 100644 --- a/src/utils/function.js +++ b/src/utils/function.js @@ -289,9 +289,12 @@ async function isUserInTargetServer(uid, id_server) { const docSnap = await getDoc(userRef) if (docSnap.exists()) { - const serversList = docSnap.data().serversid - const isAlreadyIn = serversList.includes(id_server) - return isAlreadyIn + const serversList = docSnap.data()?.serversid + if (serversList) { + const isAlreadyIn = serversList.includes(id_server) + return isAlreadyIn + } + return false } return false }