Fireship · @fireship

Role-Based Authorization with Firestore

Uploaded December 8, 2017 · Length 10:33

Role-Based Authorization with Firestore

Summary

Implement role-based authorization with Firestore using Angular and Google OAuth for user authentication, securing data access with Firestore rules and Angular abilities.

  • role-based authorization
  • firestore security
  • angular authentication
  • google oauth
  • angular abilities
  • firestore rules

Transcript preview

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

  1. 0:00

    rule-based authorization is the

  2. 0:02

    cornerstone of any user moderated site a

  3. 0:04

    good example is Stack Overflow which

  4. 0:06

    provides users various privileges based

  5. 0:08

    on the amount of reputation that they've

  6. 0:09

    gained on the site in this episode I'm

  7. 0:11

    going to show you how to use angular 5

  8. 0:13

    and firestore to build your own

  9. 0:14

    role-based authorization feature my goal

  10. 0:17

    is to give you a solution that is both

  11. 0:18

    flexible and secure on the front end and

  12. 0:21

    the back end it works by assigning a

  13. 0:23

    role to a user that saved in fire store

  14. 0:25

    then that role is used to walk down

  15. 0:26

    certain actions in angular the default

  16. 0:29

    role is a subscriber which only has the

  17. 0:30

    ability to read data then we have an

  18. 0:32

    editor that can read and update data and

  19. 0:35

    of course an admin user that can read

  20. 0:37

    update create and delete if you're brand

  21. 0:39

    new to the channel make sure to

  22. 0:40

    subscribe and you can follow along with

  23. 0:42

    the source code at angular firebase com

  24. 0:44

    I'm starting from a brand new angular

  25. 0:46

    app with angular fire two installed you

  26. 0:48

    can find the install instructions on the

  27. 0:50

    official repo the first thing I'm going

  28. 0:52

    to do is generate a core module to

  29. 0:53

    isolate all the logic for authentication

  30. 0:56

    and authorization then inside that

  31. 0:58

    module I'm going to generate a service

  32. 1:00

    called the auth service now before we

  33. 1:03

    get into the actual auth code I want to

  34. 1:04

    warn you that I'm going to go through it

  35. 1:06

    very quickly because I covered it in

  36. 1:08

    episode 55 which was fires store Roth

  37. 1:11

    if you get lost refer back to that but

  38. 1:13

    let's quickly review the sign-in signup

  39. 1:15

    process first I'm going to import our

  40. 1:17

    angular fire libraries and then I'm

  41. 1:19

    going to define a custom user interface

  42. 1:21

    but before we define the user let's

  43. 1:24

    actually define our roles as I mentioned

  44. 1:26

    before the three roles that we have our

  45. 1:28

    subscriber editor and admin the roles

  46. 1:32

    will be saved as an object in fire store

  47. 1:33

    with each possible role being set to a

  48. 1:35

    boolean value this allows you to assign

  49. 1:38

    multiple roles to a single user if

  50. 1:39

    that's what you desire then on the user

  51. 1:42

    itself we'll just have a user ID and

  52. 1:44

    email and then the roles property will

  53. 1:46

    be set to this roles object interface

  54. 1:48

    let's go ahead and switch back to the

  55. 1:51

    service and then we can set up an

  56. 1:52

    observable that is type two this user

  57. 1:54

    interface to authenticate a user and use

  58. 1:58

    that fire store document we'll first