File

src/app/user/auth-guard.service.ts

Index

Methods

Constructor

constructor(authService: AuthService, router: Router)
Parameters :
Name Type Optional
authService AuthService No
router Router No

Methods

canActivate
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
Parameters :
Name Type Optional
route ActivatedRouteSnapshot No
state RouterStateSnapshot No
Returns : boolean
checkLoggedIn
checkLoggedIn(url: string)
Parameters :
Name Type Optional
url string No
Returns : boolean
import { CanActivate } from '@angular/router';
import { Injectable } from '@angular/core';
import { AuthService } from 'src/app/user/auth.service';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Router } from '@angular/router';

@Injectable({
    providedIn:'root'
})
export class AuthGuard implements CanActivate{

    constructor(private authService: AuthService,
    private router: Router){

    }
    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean{
        return this.checkLoggedIn(state.url);      
    }

    checkLoggedIn(url: string): boolean{
        if( this.authService.isLoggedIn())
        return true;

         // Retain the attempted URL for redirection
         this.authService.redirectUrl = url;
         this.router.navigate(['kycform/login']);
         return false;
    }
}

result-matching ""

    No results matching ""