From 12b82d84c16f59dd64fbe20f50f7102dcb7c4106 Mon Sep 17 00:00:00 2001 From: NyxiumYuuki Date: Tue, 1 Jun 2021 22:31:39 +0200 Subject: [PATCH] AuthGuard added --- frontend/src/app/auth.guard.spec.ts | 16 ++++++++++++++++ frontend/src/app/auth.guard.ts | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 frontend/src/app/auth.guard.spec.ts create mode 100644 frontend/src/app/auth.guard.ts 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; + } + +}