0

i am trying to create a user collection in firebase firestore after when user sign in with google. i use these lines of code

const signInWithGoogle = async () => {
    const provider = new fb.auth.GoogleAuthProvider();
    fb.auth().useDeviceLanguage();
    try {
      await fb.auth().signInWithRedirect(provider)
      fb.auth().getRedirectResult().then(function(result) {
        console.log("user sign in", result)
        DB.collection("users").add({
          username: result.additionalUserInfo.profile.given_name,
          name : result.user.displayName,
          photo: result.user.photoURL,
          email: result.user.email,
          uid: result.user.uid,
        });
      }).catch(function (error) {
      console.log("error", error.message)
    });
    } catch (error) {
      console.log(error.message);
    }
  };

but when i click on sign in button, user get sign in but after the signin this code could run

.then(function(result) {
        console.log("user sign in", result)
        DB.collection("users").add({
          username: result.additionalUserInfo.profile.given_name,
          name : result.user.displayName,
          photo: result.user.photoURL,
          email: result.user.email,
          uid: result.user.uid,
        });
      }).catch(function (error) {
      console.log("error", error.message)
    })

but that code , did not called after user sign in. neither console.log("user sign in",result) msg show in console section

10
  • 1
    You're not checking the result of the call to add(). It could be failing and you'd never know why. Check for errors and see what happens. Commented May 4, 2022 at 13:16
  • It don't gives any kind of error. Not in console , not in terminal , not in web screen. It only return home page after signin. Commented May 7, 2022 at 2:50
  • Did you follow this document? Commented May 8, 2022 at 7:36
  • i have tried that too . but it's not working Commented May 8, 2022 at 8:22
  • Can you add a catch block after then to show the error; so have tow catch. Just try Commented May 8, 2022 at 8:53

1 Answer 1

1

so this is the previous code , i had used

const signInWithGoogle = async () => {
    const provider = new fb.auth.GoogleAuthProvider();
    fb.auth().useDeviceLanguage();
    try {
      await fb.auth().signInWithRedirect(provider)
      fb.auth().getRedirectResult().then(function(result) {
        console.log("user sign in", result)
        DB.collection("users").add({
          username: result.additionalUserInfo.profile.given_name,
          name : result.user.displayName,
          photo: result.user.photoURL,
          email: result.user.email,
          uid: result.user.uid,
        });
      }).catch(function (error) {
      console.log("error", error.message)
    });
    } catch (error) {
      console.log(error.message);
    }
  };

But the problem was that the second code(getRedirectResult()) was executed while the first code (signInWithRedirect(provider) was running. so its obvious that the second code will give null value in result. because first code is not done.

To solve that problem , i separate these codes in diff files. so in 'signin.js' file i write this code.

const signInWithGoogle = async () => {
    try {
      const provider = new fb.auth.GoogleAuthProvider();
      fb.auth().signInWithRedirect(provider) 
    } catch (error) {
      console.log(error.message);
    }
  }

and the rest of the code is written in the "App.js" file.

fb.auth().getRedirectResult().then(function(result) {
    DB.collection("users").add({
      username: result.additionalUserInfo.profile.given_name,
      name : result.user.displayName,
      photo: result.user.photoURL,
      email: result.user.email,
      uid: result.user.uid,
  });
}); 

And doing that solves the problem.

i recorded a youtube video also for this solution . you can watch here https://youtu.be/EKHk8tpd7dI

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.