Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Created October 29, 2021 10:03
Show Gist options
  • Save NyaGarcia/45487b4e9d99d4f8a845f6c80957014e to your computer and use it in GitHub Desktop.
Save NyaGarcia/45487b4e9d99d4f8a845f6c80957014e to your computer and use it in GitHub Desktop.
Using the AngularFire canActive helper to make the routes more readable
import { RouterModule, Routes } from '@angular/router';
import {
canActivate,
redirectLoggedInTo,
redirectUnauthorizedTo,
} from '@angular/fire/auth-guard';
import { NgModule } from '@angular/core';
const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(['']);
const redirectLoggedInToHome = () => redirectLoggedInTo(['dashboard']);
const routes: Routes = [
{
path: '',
loadChildren: () =>
import('./features/auth/auth.module').then((m) => m.AuthModule),
...canActivate(redirectLoggedInToHome),
},
{
path: 'dashboard',
loadChildren: () =>
import('./features/dashboard/dashboard.module').then(
(m) => m.DashboardModule
),
...canActivate(redirectUnauthorizedToLogin),
},
{
path: '**',
redirectTo: '',
pathMatch: 'full',
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment