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
How to make separate login for Users and Freelancer based on Roles that is in my real time database Firebase

Hello I am working a web application with Firebase Realtime Database and Authentication with nodejs or javascript.

This is my real time database and I want to make a login form which if the User = User he will go to User Index and if the User = Freelancer he will go to Freelancer Index.

And this is the line of code that I was tweaking or trying but It doesn't go in my way.

        <script>

            firebase.auth().onAuthStateChanged(function(user)
            {
                if(user)
                {
                    var userID = firebase.auth().currentUser.uid;

                    firebase.database().ref('Users/' + userID).once('value').then(function(snapshot)
                    {
                        if (snapshot.val())
                        {
                            window.location.href = "index.html";
                        }
                        else
                        {
                            window.location.href = "okay.html";
                        }
                    });

                }
            });

        </script>

Hoping I can get feedbacks or answers here. I am almost trying it for 2days already that's why I seek help here. Comments and answers are highly appreciated thank you!

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

0

With your current data structure you will need to check in two places to determine what role a user has. While this technically possible, it is less efficient and more complex in code.

I recommend instead to store a single top-level node (say UserRoles) where you simply keep the role for each UID:

"UserRoles": {
  "uidOfUser1": "Freelancer",
  "uidOfUser2": "User"
}

With that in place, you can load it in your onAuthStateChanged callback with:

const ref = firebase.database.ref("UserRoles");
ref.child(user.uid).once("value").then((snapshot) => {
  if (snapshot.val() === "Freelancer") {
    window.location.href = "okay.html";
  }
  else if (snapshot.val() === "User") {
    window.location.href = "index.html";
  }
  else {
    alert("Can't determine role for user: "+user.uid)
  }
});
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