X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=blueprints%2Fcommon%2Feliot-ui%2Ffrontend-src%2Fsrc%2Fapp%2F_services%2FAuthGuard.ts;fp=blueprints%2Fcommon%2Feliot-ui%2Ffrontend-src%2Fsrc%2Fapp%2F_services%2FAuthGuard.ts;h=7177e1128516bb51d681ec676e8f3862a1b4d4e3;hb=5c2048d4d3e672783eea4f306aa9a03f33a1a9f2;hp=0000000000000000000000000000000000000000;hpb=59f512662c02a16c1d5c45b090b185d2e773310f;p=eliot.git diff --git a/blueprints/common/eliot-ui/frontend-src/src/app/_services/AuthGuard.ts b/blueprints/common/eliot-ui/frontend-src/src/app/_services/AuthGuard.ts new file mode 100644 index 0000000..7177e11 --- /dev/null +++ b/blueprints/common/eliot-ui/frontend-src/src/app/_services/AuthGuard.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@angular/core'; +import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; + +import { AuthenticationService } from './../_services/authentication.service'; + +@Injectable({ providedIn: 'root' }) + +export class AuthGuard implements CanActivate { + + constructor( + private router: Router, + private authenticationService: AuthenticationService + ) { } + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + const currentUser = this.authenticationService.currentUserValue; + + console.log("current user"); + console.log(currentUser); + + if (currentUser) { + // authorised so return true + return true; + } + + // not logged in so redirect to login page + this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } }); + return false; + } + +} \ No newline at end of file