Fireship · @fireship
Angular Router Guards for Firebase Users
Uploaded April 19, 2017 · Length 1:44
Summary
use angular router guards to prevent unauthorized firebase users from accessing routes
- angular router guards
- firebase authentication
- angular routing
- angular firebase
- route protection
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
router guards are used in angular to
- 0:02
prevent unauthorized users from
- 0:04
navigating anywhere they want in your
- 0:06
app at this point our app has a working
- 0:09
fire based authentication system but any
- 0:12
visitor can access routes intended only
- 0:14
for logged in users guards handled this
- 0:17
problem by implementing the can activate
- 0:19
interface which tells angular whether or
- 0:22
not to activate a certain route in this
- 0:25
example we will protect pages from
- 0:27
unauthenticated users and redirect them
- 0:30
to a sign-in page we use the CLI and
- 0:36
angular 4 to generate a default guard
- 0:39
guards use the injectable decorator
- 0:42
so they need to be registered as
- 0:43
provider just like any other service and
- 0:46
the app module our users are
- 0:51
authenticating via firebase and angular
- 0:52
fire to package in the service we are
- 0:56
subscribing the firebase off-state than
- 0:59
using a getter to see if the auth object
- 1:00
is null check out the OAuth tutorial for
- 1:04
more details on this code router guards
- 1:08
always return a boolean type or boolean
- 1:11
observable the logic here is simple if
- 1:13
the firebase off state is defined we
- 1:16
activate the route otherwise we return
- 1:18
false and navigate the user to the login
- 1:20
page
- 1:23
now that the guard is complete we just
- 1:26
need to apply it to specific routes in
- 1:28
the routing module that's it for router
- 1:30
guards I'll see you next time
- 1:32
[Music]
- 1:34
Oh
- 1:37
[Music]


