How to "chain" AngularFire guards?

228 views
Skip to first unread message

Matt Penner

unread,
Jan 4, 2020, 6:30:27 PM1/4/20
to Firebase Google Group
I'm sure there is a better way to do this but I can't seem to wrap my head around it right now.

We have several admin areas that only those in an admin role should get to, and have a custom standard guard for this.

We are also using AngularFire guards for general authenticated user routes that redirect to the login page if you are not logged in.

Right now, if you attempt to go to the admin route but are not an admin, you are not redirected.

For a really clean flow I would like the following to happen when attempting to access an admin route:
  • If you pass the admin guard (are an admin role) allow you access
  • If you are not an admin, but are logged in, send you to your user dashboard
  • If you are not logged in send you to the login page
Right now I am chaining my guards like the following:
  {
    path: 'some-admin-path',
    loadChildren: () => import('../components/some-admin.module')
      .then(m => m.SomeAdminModule),
      canActivate: [AdminGuard], ...canActivate(redirectLoggedInToDashboard), ...canActivate(redirectUnauthorizedToLogin)
    },
  {

However, this is a bit long and can be messy to use for all of our admin guards.  How would I wrap all this up into a single AngularFire guard constant that I can use in the ...canActivate method?

Kato Richardson

unread,
Jan 6, 2020, 11:39:36 AM1/6/20
to Firebase Google Group
Hi Matt,

What happens inside your admin module right now? Not clear what you're looking to consolidate here. If you're just looking for a place to store the admin role, you can do that on user profiles in Firestore or Realtime Database (of course) or you can use custom auth claims to store them on the token.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/629625ea-3ba1-4587-952e-b2e4e7973691%40googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages