File

src/app/auth/helpers/admin.guard.ts

Index

Methods

Constructor

constructor(router: Router, authService: AuthService)
Parameters :
Name Type Optional
router Router No
authService AuthService No

Methods

canActivate
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
Parameters :
Name Type Optional
route ActivatedRouteSnapshot No
state RouterStateSnapshot No
Returns : Observable | Promise | boolean | UrlTree
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import {AuthService} from '@app/auth/services/auth.service';

@Injectable({
  providedIn: 'root'
})

export class AdminGuard implements CanActivate {

  constructor(private router: Router, private authService: AuthService) { }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    const isAdmin = this.authService.isAdmin;
    if (isAdmin) {
      return true;
    }

    this.router.navigate(['projects']);
    return false;
  }
}

result-matching ""

    No results matching ""