Learn Courses My Dashboard

How to Login Users with different roles in iOS with firebase

Hi I have a questions have a firestore db and several users also I have created different pages for admin and user,My question is I want a normal user to see homepage1 and admin to see homepage2,kindly how do I effect this?For now I only have the normal login giving success message.

Kindly help and also with an illustration of code if possible

Kind regards,
Mary.

Hi Mary, you will need to use Firestore or the real-time database to create users and give them roles and associate their UUID

All users will be authenticated with Firebase, but you can use the database to create “roles”

1 Like

Hi Mikaela,thanks I am using Firestore database and I have several users in just that I don’t know how to log in admin user and normal user how do I go about it?

You log them both in the same way. This is called authentication (are you who you say you are)

You’re now doing authorization (what can you access)

You can save the data in Firestore and fetch it after successful login, and based on the result, determine if they’re an admin or normal user

Firestore:

  • users
    • user UUID
    • role: “admin or normal”
    • other user info
1 Like

Hi Mikaela,thanks I understand I have uuid but how do I set roles?I have tried search on how to but don’t seem to get it through…also do you have a sample code to the problem?

You set the role like I’ve shown, in the example of what should be in Firestore, when you create your user.

Later on when you authenticate, you read the database and read which role a user has

I don’t have same code because there’s a million ways to do this.

Your data In Firebase looks like this:

You set the role as a string when you create your initial user.

Later when you login, you read this information from Firebase for the user who’s logging in, and determine what role they have

Here’s one general way to do it that is using security rules to accomplish who can do what

https://firebase.google.com/docs/firestore/solutions/role-based-access