Skip to content

Instantly share code, notes, and snippets.

@theoomoregbee
Created August 3, 2018 04:39
Show Gist options
  • Save theoomoregbee/2e536a77e538713660d2c8e9d70f8e2c to your computer and use it in GitHub Desktop.
Save theoomoregbee/2e536a77e538713660d2c8e9d70f8e2c to your computer and use it in GitHub Desktop.
Role guard for guard post
import { AuthService } from './../services/auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, Route } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable()
export class RoleGuard implements CanActivate {
constructor(private _authService: AuthService, private _router: Router) {
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
const user = this._authService.decode();
if (user.Role === next.data.role) {
return true;
}
// navigate to not found page
this._router.navigate(['/404']);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment