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

165
Visualizações
Firebase auth + firestore (what is the proper way to "link" users)

I'm making smth with backed for the first time in my life, so I'm sorry in advance, I'm making a web chat app. I think I managed to deal with authentication (it seems to be working) and now I want to make connect somehow the authentication user names with chat users... so I tried to

 const docRef = await addDoc(collection(database, 'users'), {
        name: user.displayName,
    });
    console.log('Document written with ID: ', docRef.id);
} catch (e) {
    console.error('Error adding document: ', e);
}

but it says user not defined because userCredentials is in the scope of authentications functions... If I paste this code into some function where userCredentials can be found, it says there is some problem with await word...

I want to take userCredential that logged in and use it in the chat app... so I need to link somehow the auth db and the firestore db? or is it done completely differently? Thank you

Could you give a bit of advice? Thank you (edited)

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

0

If you want to use the user's name, you first need to make sure that your code only runs once the user is signed in. In Firebase you can do that as shown in the documentation on getting the currently signed in user:

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // 👇 your application specific code is below
    const docRef = await addDoc(collection(database, 'users'), {
      name: user.displayName,
    });
    console.log('Document written with ID: ', docRef.id);
  } else {
    // User is signed out
    // ...
  }
})

I'd actually recommend not using addDoc, but basing the document ID on the UID of the user, like this:

if (user) {
  //           👇 get UID from user
  const uid = user.uid;

  // Use as document ID 👇     👇                     👇 
  const docRef = await setDoc(doc(database, 'users', uid), {
    name: user.displayName,
  });

Since document IDs are unique within a collection, this automatically ensures that each user can only have one document in the users collection. It also makes it easier to find a user's document when needed, and makes it easier to ensure users can only edit their own document.

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