You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

24 lines
806 B

  1. import { Injectable } from '@angular/core';
  2. import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
  3. import { AccountService } from '@app/_services';
  4. @Injectable({ providedIn: 'root' })
  5. export class AuthGuard implements CanActivate {
  6. constructor(
  7. private router: Router,
  8. private accountService: AccountService
  9. ) {}
  10. canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  11. const user = this.accountService.userValue;
  12. if (user) {
  13. // authorised so return true
  14. return true;
  15. }
  16. // not logged in so redirect to login page with the return url
  17. this.router.navigate(['/account/login'], { queryParams: { returnUrl: state.url }});
  18. return false;
  19. }
  20. }