ajout de auth guard
parent
759a89dce5
commit
a72b86db52
@ -0,0 +1,15 @@
|
||||
import { TestBed, async, inject } from '@angular/core/testing';
|
||||
|
||||
import { AuthGuard } from './auth.guard';
|
||||
|
||||
describe('AuthGuard', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [AuthGuard]
|
||||
});
|
||||
});
|
||||
|
||||
it('should ...', inject([AuthGuard], (guard: AuthGuard) => {
|
||||
expect(guard).toBeTruthy();
|
||||
}));
|
||||
});
|
@ -0,0 +1,21 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { AuthService } from '../services/auth/auth.service';
|
||||
import { Router } from '@angular/router';
|
||||
@Injectable()
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(private auth: AuthService, private router: Router){}
|
||||
canActivate(
|
||||
next: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
|
||||
|
||||
if (this.auth.authenticated){
|
||||
return true;
|
||||
}
|
||||
|
||||
console.log('access denied!');
|
||||
this.router.navigate(['/']);
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue