Helloo,
I have created a guard:
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
    constructor(private router: Router) { 
    }
    canActivate() {
        if (localStorage.getItem('currentUser')) {
            // logged in so return true
            return true;
        }
        // not logged in so redirect to login page
        this.router.navigate(['/login']);
        return false;
    }
}
and have multiple modules with multiple routes inside them. How do I easily restrict every route in my app with this guard?
Best regards
Setup an empty route with guard, and make the rest of your routes children of that one:
RouterModule.forRoot([
  { path: '', canActivate: [AuthGuard], children: [...restOfYourRoutes] }])
                        You can use componentless routes
{ path: '', canActivate: [MyGuard], children: [
   {path: 'x1', ...},
   {path: 'x2', ...},
MyGuard will be applied to all child routes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With