Loading src/app/dashboard/auth-guard.service.spec.ts 0 → 100644 +12 −0 Original line number Diff line number Diff line import { TestBed } from '@angular/core/testing'; import { AuthGuard } from './auth-guard.service'; describe('AuthGuardService', () => { beforeEach(() => TestBed.configureTestingModule({})); it('should be created', () => { const service: AuthGuard = TestBed.get(AuthGuard); expect(service).toBeTruthy(); }); }); src/app/dashboard/auth-guard.service.ts 0 → 100644 +25 −0 Original line number Diff line number Diff line import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {Injectable} from '@angular/core'; import {AuthService} from '../auth.service'; @Injectable() export class AuthGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { return this.authService.isAuthenticated() .then( (authenticated: boolean) => { if (authenticated) { return true; } else { this.router.navigate(['/login']); } } ); } } No newline at end of file Loading
src/app/dashboard/auth-guard.service.spec.ts 0 → 100644 +12 −0 Original line number Diff line number Diff line import { TestBed } from '@angular/core/testing'; import { AuthGuard } from './auth-guard.service'; describe('AuthGuardService', () => { beforeEach(() => TestBed.configureTestingModule({})); it('should be created', () => { const service: AuthGuard = TestBed.get(AuthGuard); expect(service).toBeTruthy(); }); });
src/app/dashboard/auth-guard.service.ts 0 → 100644 +25 −0 Original line number Diff line number Diff line import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {Injectable} from '@angular/core'; import {AuthService} from '../auth.service'; @Injectable() export class AuthGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { return this.authService.isAuthenticated() .then( (authenticated: boolean) => { if (authenticated) { return true; } else { this.router.navigate(['/login']); } } ); } } No newline at end of file