Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { AuthenticationService } from '../services/authentication.service';
- import { Injectable } from '@angular/core';
- import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, Route } from '@angular/router';
- import { Observable } from 'rxjs';
- @Injectable()
- export class AuthGuard implements CanActivate {
- constructor(private _authService:AuthenticationService , private _router: Router) {
- }
- canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
- // implement check function in your auth service
- if (this._authService.isLoggedIn()) {
- return true;
- }
- // navigate to login page
- this._router.navigate(['/login']);
- // you can save redirect url so after authing we can move them back to the page they requested
- return false;
- }
- }
Add Comment
Please, Sign In to add comment