File

src/app/auth/auth.guard.ts

Description

AuthGuard is responsible for navigating the user to /login when he is not currently logged in according to the AuthenticationsService. It's canActivate method is automatically invoked by angular on routing events.

Index

Methods

Constructor

constructor(router: Router, authenticationService: AuthenticationService)
Parameters :
Name Type Optional
router Router No
authenticationService AuthenticationService No

Methods

canActivate
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)

Redirects user to login if he is not authenticated.

Parameters :
Name Type Optional Description
route ActivatedRouteSnapshot No

unused but required for CanActivate signature

state RouterStateSnapshot No

router state when guard was invoked, used to redirect the user after login

Returns : boolean

true iff the user is logged in according to the AuthenticationService

import {Injectable} from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {AuthenticationService} from './authentication.service';

/** AuthGuard is responsible for navigating the user to /login when he is not
 * currently logged in according to the AuthenticationsService. It's canActivate
 * method is automatically invoked by angular on routing events.
 */
@Injectable({providedIn: 'root'})
export class AuthGuard implements CanActivate {
  constructor(private router: Router, private authenticationService: AuthenticationService) {}

  /**
   * Redirects user to login if he is not authenticated.
   * @param route unused but required for CanActivate signature
   * @param state router state when guard was invoked, used to redirect the user after login
   * @returns true iff the user is logged in according to the AuthenticationService
   */
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    const currentUser = this.authenticationService.currentUserValue;
    if (currentUser) {
      // logged in so return true
      return true;
    }
    // not logged in so redirect to login page with the return url
    this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
    return false;
  }
}

results matching ""

    No results matching ""