argos/dmp-frontend/src/app/guards/auth.guard.ts

21 lines
727 B
TypeScript
Raw Normal View History

2017-09-26 17:16:04 +02:00
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
2017-11-01 18:18:27 +01:00
import { TokenService, TokenProvider } from '../services/login/token.service';
2017-09-26 17:16:04 +02:00
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private tokenService: TokenService) { }
2017-09-26 17:16:04 +02:00
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
2017-09-26 17:16:04 +02:00
2017-11-01 18:18:27 +01:00
if(this.tokenService.isLoggedIn() == true){
return true;
}
2017-09-26 17:16:04 +02:00
// not logged in so redirect to login page with the return url
2017-11-02 10:11:29 +01:00
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
2017-09-26 17:16:04 +02:00
return false;
2017-11-01 18:18:27 +01:00
2017-09-26 17:16:04 +02:00
}
2017-11-01 18:18:27 +01:00
}