Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up roles in firebase auth?

I am currently logging in with email and password. But I would like to be able to assign a role to each user: admin or user. I have read that this is done by using the custom auth method, but I find the docs not clear to implement with email/password authentication.

How would I go to set that up?

I am currently using firebase for ember emberfire

Update:

Docs reference: https://www.firebase.com/docs/web/guide/login/custom.html

like image 481
FutoRicky Avatar asked Apr 27 '16 00:04

FutoRicky


People also ask

What does Firebase auth () do?

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.

What does Firebase auth () CurrentUser return?

What does Firebase auth () CurrentUser return? If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing.


1 Answers

Firebase just launched support for role based access on any user via custom user claims on the ID token: https://firebase.google.com/docs/auth/admin/custom-claims

You would define the admin access rule:

{
  "rules": {
    "adminContent": {
      ".read": "auth.token.admin === true",
      ".write": "auth.token.admin === true",
    }
  }
}

Set the user role with the Admin SDK:

// Set admin privilege on the user corresponding to uid.
admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
});

This will propagate to the corresponding user's ID token claims.

To parse it from the token on the client, check: https://firebase.google.com/docs/auth/admin/custom-claims#access_custom_claims_on_the_client

like image 136
bojeil Avatar answered Dec 10 '22 13:12

bojeil