This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
FoodLoop-Web/src/app/_guards/auth.guard.ts

22 lines
684 B
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (localStorage.getItem('sessionKey')) {
2017-09-19 10:09:30 +00:00
console.log('session key found');
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
2017-09-19 10:09:30 +00:00
console.log('no session key found');
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}
}