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

124
Vistas
Access Firebase User information before creating user

I am creating the authentication side of a web app using Firebase's Google Sign in and would like to see if the newUser.displayName is unique in the Firestore database. My current code doesn't work as I must create the user before accessing the displayName. If I create the user first and then check the displayName, that would cause problems as I would need to go back and delete the user if the condition is not met. Is there a way to access displayName before the user is created?

Signup.js

const handleGoogleSubmit = async (e) => {
        e.preventDefault();
        setError('');
        try {
            const available = await usernameAvailability(newUser.user.displayName.replace(/\s/g, ""))
            const newUser = await signInWithGoogle(email, password);
            const { isNewUser } = getAdditionalUserInfo(newUser) 

            if (isNewUser) {
                await setDoc(doc(db, "users", newUser.user.uid), {
                    email: newUser.user.email,
                    uid: newUser.user.uid,
                    username: newUser.user.displayName.replace(/\s/g, ""),
                    usernameLowercase: newUser.user.displayName.toLowerCase(),
                });
            }
            navigate('/account');
        } catch (error) {
            setError(error.message);
            console.log(error.message);
        }
    }

AuthContext.js

const usernameAvailability = async (username) => {
        const q = query(collection(db, "users"), where("usernameLowercase", "==", username.toLowerCase()));
        const querySnapshot = await getDocs(q)
        console.log(querySnapshot.empty, 'querySnapshot.empty')
        if (querySnapshot.empty) {
            return true
        } else {
            throw new Error("Username already exists")
        }
    }
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

There is no way to query Firebase Authentication by the display name of the users, nor any way to a priori ensure that display names are unique. That is not their purpose, so I'd recommend not trying to make it do something it wasn't meant for.

The common approach to having unique user names is to store them in some cloud-hosted database (like Realtime Database or Firestore in Firebase, but others are also fine) and querying/claiming a name there before calling the Firebase API to create the user there.

This topic has been covered quite a few times before, so I recommend checking out these search results, such as:

  • Firebase Auth Is there a way to make sure displayName is unique between all users?
  • Firebase android : make username unique
  • Username authentication instead of email
  • Cloud Firestore: Enforcing Unique User Names
about 4 years ago · Santiago Trujillo 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