Fireship · @fireship

Role-Based User Permissions in Firebase

Uploaded August 16, 2017 · Length 7:52

Role-Based User Permissions in Firebase

Summary

Implement role-based user permissions in Firebase using Angular and Firebase Authentication.

  • firebase role-based permissions
  • angular firebase authentication
  • firebase user permissions
  • firebase database security
  • firebase authorization

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    role-based user authorization can give

  2. 0:02

    you fine-grained control over which

  3. 0:04

    actions a user can perform in your app

  4. 0:06

    many apps use this strategy such as

  5. 0:08

    Stack Overflow and reddit to control the

  6. 0:11

    types of data that can be modified by

  7. 0:13

    users in this episode we're going to

  8. 0:15

    implement role based access control with

  9. 0:17

    firebase and angular core our users are

  10. 0:20

    going to have three different roles the

  11. 0:21

    admin role can read and write anything

  12. 0:23

    author role can read and edit but not

  13. 0:26

    delete and the reader can only read data

  14. 0:28

    since we can't actually edit the data on

  15. 0:31

    the firebase object we need to create

  16. 0:33

    our own custom user class will do this

  17. 0:36

    by saving our own custom data to the

  18. 0:37

    real-time database and in the user

  19. 0:39

    object will have a roles object which

  20. 0:42

    can either have an author reader or

  21. 0:44

    admin attribute in the angular app I'm

  22. 0:46

    going to use a typescript interface to

  23. 0:48

    make sure that the user roles can't be

  24. 0:50

    anything other than a reader author or

  25. 0:52

    admin then I'm creating a custom user

  26. 0:55

    class which is where we can define any

  27. 0:57

    custom data we want to add to the user

  28. 0:58

    object by default will pass the

  29. 1:01

    constructor the firebase auth data and

  30. 1:03

    use that to set an email photo URL and

  31. 1:06

    then we'll set the reader value to true

  32. 1:08

    by default so a new user will look like

  33. 1:10

    this when they sign up in the database

  34. 1:12

    so now we can build an off service to

  35. 1:14

    actually log in and log out the user

  36. 1:16

    we're going to use Google OAuth as the

  37. 1:18

    login method so we import the

  38. 1:20

    angularfire database as well as

  39. 1:22

    angularfire

  40. 1:22

    auth and our custom user class and we're

  41. 1:26

    going to set our current user as a

  42. 1:27

    behavior subject instead of the

  43. 1:29

    angularfire

  44. 1:29

    auth observable this will give us more

  45. 1:31

    flexibility when we start working with

  46. 1:33

    router guards inside the service

  47. 1:35

    constructor we subscribe to the

  48. 1:37

    angularfire auth off state observable

  49. 1:39

    and we can add switch map which will

  50. 1:42

    give us access to the auth user ID and

  51. 1:44

    we can return the location in the

  52. 1:46

    database that has this user data which

  53. 1:48

    in this case is a user collection

  54. 1:50

    followed by the user ID if the auth

  55. 1:53

    object is undefined we just return a

  56. 1:54

    null observable then we can call

  57. 1:57

    subscribe and whatever we get back is

  58. 1:59

    what will apply to that behavior subject