Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda