Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { CanActivate, Router, CanLoad, Route, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router';
- import { Injectable } from '@angular/core';
- import { AuthService } from './auth.service';
- import { Observable } from 'rxjs';
- @Injectable({
- providedIn: 'root'
- })
- export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
- constructor(private authService: AuthService, private router: Router) {
- }
- checkAuth() {
- if ( this.authService.isAuth() ) {
- return true;
- }
- else {
- this.router.navigate(['login']);
- }
- }
- canLoad(route: Route): Observable<boolean>|Promise<boolean>|boolean {
- return this.checkAuth();
- }
- canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
- return this.checkAuth();
- }
- canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
- return this.checkAuth();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement