ng-pokemon-app/src/app/auth.guard.ts

18 lines
452 B
TypeScript

import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate() {
if (this.authService.isLoggedIn) {
return true;
}
this.router.navigate(['/login']);
return false;
}
}