diff --git a/frontend/src/app/auth.guard.spec.ts b/frontend/src/app/auth.guard.spec.ts new file mode 100644 index 0000000..68889d2 --- /dev/null +++ b/frontend/src/app/auth.guard.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthGuard } from './auth.guard'; + +describe('AuthGuard', () => { + let guard: AuthGuard; + + beforeEach(() => { + TestBed.configureTestingModule({}); + guard = TestBed.inject(AuthGuard); + }); + + it('should be created', () => { + expect(guard).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/auth.guard.ts b/frontend/src/app/auth.guard.ts new file mode 100644 index 0000000..6eaec40 --- /dev/null +++ b/frontend/src/app/auth.guard.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core'; +import {ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, CanActivateChild, Router} from '@angular/router'; +import { Observable } from 'rxjs'; +import {AuthService} from './services/auth/auth.service'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthGuard implements CanActivateChild { + + constructor(private authService: AuthService, private router: Router) { + } + + canActivateChild( + childRoute: ActivatedRouteSnapshot, + state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { + if(!this.authService.getLogged()){ + this.router.navigate(['login']); + return false; + } + return true; + } + +}