Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!