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

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

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