Fireship · @fireship
Role-Based Authorization with Firestore
Uploaded December 8, 2017 · Length 10:33
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.
- 0:00
rule-based authorization is the
- 0:02
cornerstone of any user moderated site a
- 0:04
good example is Stack Overflow which
- 0:06
provides users various privileges based
- 0:08
on the amount of reputation that they've
- 0:09
gained on the site in this episode I'm
- 0:11
going to show you how to use angular 5
- 0:13
and firestore to build your own
- 0:14
role-based authorization feature my goal
- 0:17
is to give you a solution that is both
- 0:18
flexible and secure on the front end and
- 0:21
the back end it works by assigning a
- 0:23
role to a user that saved in fire store
- 0:25
then that role is used to walk down
- 0:26
certain actions in angular the default
- 0:29
role is a subscriber which only has the
- 0:30
ability to read data then we have an
- 0:32
editor that can read and update data and
- 0:35
of course an admin user that can read
- 0:37
update create and delete if you're brand
- 0:39
new to the channel make sure to
- 0:40
subscribe and you can follow along with
- 0:42
the source code at angular firebase com
- 0:44
I'm starting from a brand new angular
- 0:46
app with angular fire two installed you
- 0:48
can find the install instructions on the
- 0:50
official repo the first thing I'm going
- 0:52
to do is generate a core module to
- 0:53
isolate all the logic for authentication
- 0:56
and authorization then inside that
- 0:58
module I'm going to generate a service
- 1:00
called the auth service now before we
- 1:03
get into the actual auth code I want to
- 1:04
warn you that I'm going to go through it
- 1:06
very quickly because I covered it in
- 1:08
episode 55 which was fires store Roth
- 1:11
if you get lost refer back to that but
- 1:13
let's quickly review the sign-in signup
- 1:15
process first I'm going to import our
- 1:17
angular fire libraries and then I'm
- 1:19
going to define a custom user interface
- 1:21
but before we define the user let's
- 1:24
actually define our roles as I mentioned
- 1:26
before the three roles that we have our
- 1:28
subscriber editor and admin the roles
- 1:32
will be saved as an object in fire store
- 1:33
with each possible role being set to a
- 1:35
boolean value this allows you to assign
- 1:38
multiple roles to a single user if
- 1:39
that's what you desire then on the user
- 1:42
itself we'll just have a user ID and
- 1:44
email and then the roles property will
- 1:46
be set to this roles object interface
- 1:48
let's go ahead and switch back to the
- 1:51
service and then we can set up an
- 1:52
observable that is type two this user
- 1:54
interface to authenticate a user and use
- 1:58
that fire store document we'll first


