Guards in Angular are nothing but the functionality, logic, and code which are executed before the route is loaded or the ones leaving the route.
Different types of guards,
- CanActivate guard (e.g. it checks route access).
- CanActivateChild guard (checks child route access).
- CanDeactivate guard (prompt for unsaved changes).
- Resolve guard (pre-fetching route data).
- CanLoad guard (check before loading feature module assets).
CanActivate Guard
- Create a new service say AuthenticationGuard.service.ts as below. If the AuthenticationGuard returns false then the user is not able to navigate to Home route in below example. And further not able to see the welcome and Edit child routes.
- import { Injectable } from "@angular/core";
- import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router";
- @Injectable()
- export class AuthenticationGuard implements CanActivate {
- constructor(private authService: AuthService){}
- canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean>|Promise<boolean>|boolean {
- return this.authService.isAuthenticated();
- //Logic of authenticating user by calling some API service.
- // For e.g. Here Authservice has a isAuthenticated() method which further
- // Check user is valid or not.
- }
- }
- In Module, while creating routes set canActivate as below,
- const APP_ROUTES : Routes = [
- {path:'', component: HomeComponent, canActivate:[ AuthenticationGuard], children:[
- {path:'', component: WelcomeComponent},
- {path:'edit', component: EditComponent, }],
- {path:':id/Logout, component: LogoutComponent},
- {path:'Login’, component: LoginComponent }
- ]},
- ]
- @NgModule({
- imports:[
- RouterModule.forChild(APP_ROUTES)
- ],
- providers:[
- AuthenticationGuard
- ],
- exports:[
- RouterModule
- ]
- })
- export class MyRoutingModule{
- }
CanActivateChild Guard
- This guard checks the access on child routes. Take the above example. If the AuthenticationGuard returns false then the user is able to navigate the Home route in below example. And will be able to see its child routes. But not able to move inside welcome or Edit child routes if it returns false.
- import { Injectable } from "@angular/core";
- import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router";
- @Injectable()
- export class AuthenticationGuard implements CanActivateChild {
- constructor(private authService: AuthService){}
- CanActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean>|Promise<boolean>|boolean {
- return this.authService.isAuthenticated();
- //Logic of authenticating user by calling some API service.
- // For e.g. Here Authservice has a isAuthenticated() method which further
- // Check user is valid or not.
- }
- }
- In Module, while creating routes set canActivateChild as below,
- const APP_ROUTES : Routes = [
- {path:'', component: HomeComponent, canActivateChild:[ AuthenticationGuard], children:[
- {path:'', component: WelcomeComponent},
- {path:'edit', component: EditComponent, }],
- {path:':id/Logout, component: LogoutComponent},
- {path:'Login’, component: LoginComponent }
- ]},
- ]
- @NgModule({
- imports:[
- RouterModule.forChild(APP_ROUTES)
- ],
- providers:[
- AuthenticationGuard
- ],
- exports:[
- RouterModule
- ]
- })
- export class MyRoutingModule{
- }
ramachandran cPosted Apr 6, 2018, 8:53 AM
Nice sir, thankyou