Allen GT
Allen GT

Reputation: 21

Multiple role authentication Firebase Web

I want to authenticate users based on their role. For example, I have 3 types of role: a normal user, a premium user and an Admin. Each type of users gets redirected to a specific page when he logs in.

I only managed to make a simple authentication for only one type of users based on the official documentation. I want to know how to sign up users with different roles then log them each to a specific page.

Upvotes: 2

Views: 1035

Answers (1)

Kiana
Kiana

Reputation: 1506

The easiest way to control what experience each user sees based off a pre-existing list of "Admin" and "Premium" users would probably be with custom claims.

If you are just using the standard Firebase login, the way to do this would be with the Admin SDK.

If you want to validate users as they sign up, you'll want to use custom login system to control signups. You can also set the custom claims from there.

Once the user is logged in, you can then redirect them to the correct page.

firebase.auth().signInWithEmailAndPassword(email, password)
    .catch(function(error) {
  // Handle Errors here.
}).then(function(){
  firebase.auth().currentUser.getIdToken()
  .then((idToken) => {
     // Parse the ID token.
     const payload = JSON.parse(b64DecodeUnicode(idToken.split('.')[1]));
     // Confirm the user is an Admin.
     if (!!payload['admin']) {
       redirectAdminUI();
     }
  })
  .catch((error) => {
    console.log(error);
});

Upvotes: 3

Related Questions