Fireship · @fireship

Angular Router Guards for Firebase Users

Uploaded April 19, 2017 · Length 1:44

Angular Router Guards for Firebase Users

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.

  1. 0:00

    router guards are used in angular to

  2. 0:02

    prevent unauthorized users from

  3. 0:04

    navigating anywhere they want in your

  4. 0:06

    app at this point our app has a working

  5. 0:09

    fire based authentication system but any

  6. 0:12

    visitor can access routes intended only

  7. 0:14

    for logged in users guards handled this

  8. 0:17

    problem by implementing the can activate

  9. 0:19

    interface which tells angular whether or

  10. 0:22

    not to activate a certain route in this

  11. 0:25

    example we will protect pages from

  12. 0:27

    unauthenticated users and redirect them

  13. 0:30

    to a sign-in page we use the CLI and

  14. 0:36

    angular 4 to generate a default guard

  15. 0:39

    guards use the injectable decorator

  16. 0:42

    so they need to be registered as

  17. 0:43

    provider just like any other service and

  18. 0:46

    the app module our users are

  19. 0:51

    authenticating via firebase and angular

  20. 0:52

    fire to package in the service we are

  21. 0:56

    subscribing the firebase off-state than

  22. 0:59

    using a getter to see if the auth object

  23. 1:00

    is null check out the OAuth tutorial for

  24. 1:04

    more details on this code router guards

  25. 1:08

    always return a boolean type or boolean

  26. 1:11

    observable the logic here is simple if

  27. 1:13

    the firebase off state is defined we

  28. 1:16

    activate the route otherwise we return

  29. 1:18

    false and navigate the user to the login

  30. 1:20

    page

  31. 1:23

    now that the guard is complete we just

  32. 1:26

    need to apply it to specific routes in

  33. 1:28

    the routing module that's it for router

  34. 1:30

    guards I'll see you next time

  35. 1:32

    [Music]

  36. 1:34

    Oh

  37. 1:37

    [Music]