src/app/auth.guard.ts
constructor(dataService: ApiService, router: Router)
|
canActivate |
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
|
Returns:
boolean
|
isLogin |
isLogin(routeUrl: string)
|
Returns:
boolean
|
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {ApiService} from './api.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private dataService: ApiService, private router: Router) {
}
/*! \brief
This function checks the state of router and calls the boolean function isLogin() to check whether a user is logged in or not.
*/
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): boolean {
const routeUrl: string = state.url;
return this.isLogin(routeUrl);
}
/*! \brief
This function calls Api Service to check the login status of the user and redirects the user to appropriate routes according to status.
*/
isLogin(routeUrl: string): boolean {
if (this.dataService.isLoggedIn()) {
return true;
}
this.dataService.redirectUrl = routeUrl;
this.router.navigate(['/login'], {queryParams: {returnUrl: routeUrl}});
}
}