Home > Mobile >  Push Data to MongoDB on Google SignIn Firebase
Push Data to MongoDB on Google SignIn Firebase

Time:02-06

I wanted to write a method where onClick the google sign in starts and after successful sign in it makes a post request to my API.But the weird problem is 30% of the times the sign in data doesnt come to mongo db.I even called signout function in the catch block.Please help if someone notice any error!!


    const Hero = () => {
  const [user, setUser] = useState(null);
  const [fetchUser, setFetchUser] = useState(null);

  const handleGoogleSignIn = () => {
    const googleProvider = new GoogleAuthProvider();
    signInWithPopup(auth, googleProvider)
      .then(async (result) => {
         console.log(result);
        try {
          const { data } = await axios.post(
            "https://myAPIherokuapp.com/api/v1/9c142e80023e07c3/registerUser",
            { name: result.user.displayName, email: result.user.email }
          );
           console.log(data);
        } catch (err) {
          console.log(err);
          signOut(auth)
        }
      })
      .catch((error) => {
        console.log(error);
      });
  };

CodePudding user response:

Maybe try async/await at the handleGoogleSignIn level? e.g.

  const handleGoogleSignIn = async () => {
     const googleProvider = await new GoogleAuthProvider();
     const userResult = await signInWithPopup(auth, googleProvier);
     await axios.post('url', userResult)....

I think that should help?

  • Related