Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
How to create user collection in firestore after sign in with google?

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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda