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

125
Visualizações
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 Respostas
Responde à pergunta

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 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