Reputation: 79
I am working with firebase and reactJs. so here i am trying to signUp with firebase auth and then once the promise is fulfilled, i am trying to reach firestore database. when i do it first time, it is done perfectly but when i logout and try to create one more account, the account is created successfully but the promise to database gets stuck and then when i logout it throws the error from that promise "about the database acess". here is the code ....
import React, { useContext } from 'react';
import { Formik, Form, Field } from 'formik';
import * as Yup from 'yup';
import firebase from '../../firestore/Firestore';
import AppContext from '../../context/AppContext';
import Google from './GoogleSignIn';
const LoginSchema = Yup.object().shape({
firstName: Yup.string()
.required('Required'),
lastName: Yup.string()
.required('Required'),
email: Yup.string()
.email('Invalid email')
.required('Required'),
password: Yup.string()
.min(6, 'Password has to be atleast 6 characters long')
.required('Required'),
confirmPassword: Yup.string()
.oneOf([Yup.ref('password'), null], 'Passwords do not match')
.required('Required'),
});
const SingUp = ({ signIn }) => {
const context = useContext(AppContext);
const { state, dispatch } = context;
const db = firebase.firestore();
const signUp = (e) => {
firebase.auth().createUserWithEmailAndPassword(e.email, e.password).then((user) => {
console.log('it entered here');
db.collection("resumeData").doc(user.user.uid).set({ profiles: [context.initialState.data] }).then(()=>
{console.log('done', user.user.uid)
}).catch((x) => { console.log(x) });
db.collection("users").doc(user.user.uid).set({ firstName: e.firstName, lastName: e.lastName }).catch((x) => { console.log(x) });
}).catch(function (error) {
//error Handling
});
}
return (
<div className="">
<div className="text-xl m-2 font-medium text-center lg:text-3xl">Create new Account</div>
<Formik
initialValues={{
email: '',
password: '',
firstName: '',
lastName: '',
confirmPassword: '',
}}
validationSchema={LoginSchema}
// validator={() => ({})}
onSubmit={values => {
dispatch({ type: 'on_input', payload: { key: "errorMessage", value: null } });
signUp(values);
}}
>
{({ errors, touched }) => (
<Form>
<div className="w-full flex justify-center items-center">
<div className={`w-full flex flex-col my-2 mr-4`}>
<label className="uppercase tracking-wide text-gray-600 text-xs font-semibold mb-2 lg:text-2xl">First Name</label>
<Field placeholder="Enter First Name" required name="firstName" type="firstName" className="appearance-none block w-full bg-gray-200 text-gray-800 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500 lg:text-3xl" />
{errors.firstName && touched.firstName ? <div className="text-red-600">{errors.firstName}</div> : null}
</div>
<div className={`w-full flex flex-col my-2 ml-4`}>
<label className="uppercase tracking-wide text-gray-600 text-xs font-semibold mb-2 lg:text-2xl">Last Name</label>
<Field placeholder="Enter Last Name" required name="lastName" type="lastName" className="appearance-none block w-full bg-gray-200 text-gray-800 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500 lg:text-3xl" />
{errors.lastName && touched.lastName ? <div className="text-red-600">{errors.lastName}</div> : null}
</div>
</div>
<div className={`w-full flex flex-col my-2`}>
<label className="uppercase tracking-wide text-gray-600 text-xs font-semibold mb-2 lg:text-2xl">Email</label>
<Field placeholder="Enter Email" required name="email" type="email" className="appearance-none block w-full bg-gray-200 text-gray-800 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500 lg:text-3xl" />
{errors.email && touched.email ? <div className="text-red-600">{errors.email}</div> : null}
</div>
<div className="w-full flex justify-center items-end">
<div className={`w-full flex flex-col my-2 mr-4`}>
<label className="uppercase tracking-wide text-gray-600 text-xs font-semibold mb-2 lg:text-2xl">Password</label>
<Field placeholder="Enter Password" required name="password" type="password" className="appearance-none block w-full bg-gray-200 text-gray-800 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500 lg:text-3xl" />
{errors.password && touched.password ? <div className="text-red-600">{errors.password}</div> : null}
</div>
<div className={`w-full flex flex-col my-2 ml-4`}>
<label className="uppercase tracking-wide text-gray-600 text-xs font-semibold mb-2 lg:text-2xl">Confirm Password</label>
<Field placeholder="Confirm Passowrd" type="password" required name="confirmPassword" className="appearance-none block w-full bg-gray-200 text-gray-800 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500 lg:text-3xl" />
{errors.confirmPassword && touched.confirmPassword ? <div className="text-red-600">{errors.confirmPassword}</div> : null}
</div>
</div>
{state.loading.state ?
<div className="font-semibold text-center">{state.loading.message}</div>
: <div className="text-red-700 font-semibold text-center">{state.errorMessage}</div>}
<div className="w-full flex justify-between items-center mb-4 mt-6">
<button disabled={state.loading.state} type="submit" value="submit" className="w-full px-5 py-2 bg-blue-600 shadow-md hover:shadow-2xl rounded-lg hover:bg-blue-800 inline-flex justify-center items-center">
<span className="text-white font-semibold lg:text-2xl">Sign Up</span>
</button>
</div>
</Form>
)}
</Formik>
<Google message={'SignUp through Google'} />
<button className="text-blue-600 font-semibold mt-6 lg:text-2xl" onClick={signIn}>Already have an Account?</button>
</div>
)
}
export default SingUp;
In above SignUp function, there are two console.log for things to go smoothly.... first "It entered here" is always triggered and but the second one which is "done" does get triggered the first time but not the second time.
A little more information: i tested it a bit more and this error is only happening when ever there's a consecutive new accounts been created. Even with google.
HELP!!
Couple of days later: 50,000 people used to live here, now it's a ghost town. Anyway, i still couldn't figure out the solution but i found my way around it by not updating database right after signIn.
Upvotes: 1
Views: 263
Reputation: 26
I had a similar issue with my Vue app, and saw this thread: https://github.com/firebase/firebase-js-sdk/issues/3218. I guess this problem will be resolved on future versions, but until then, this solution seems to work: https://github.com/firebase/firebase-js-sdk/issues/3218#issuecomment-644903101. I added a timeout of 100ms between the creation of the user and the creation of the document, and it solved the issue. Hope it helps you as well.
Upvotes: 1