Files
cmc/support-portal-frontend/src/app/guard/authentication.guard.ts
2024-09-22 10:21:52 +05:30

39 lines
1.2 KiB
TypeScript

import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable} from 'rxjs';
import {NotificationService} from "../service/notification.service";
import {NotificationType} from "../notification/notification-type";
import {AuthenticationService} from "../service/authentication.service";
// import { AuthenticationService } from '../service/authentication.service';
@Injectable({
providedIn: 'root'
})
export class AuthenticationGuard implements CanActivate {
constructor(
private authenticationService: AuthenticationService,
private router: Router,
private notificationService: NotificationService
) {
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.isUserLoggedIn();
}
private isUserLoggedIn(): boolean {
if (this.authenticationService.isUserLoggedIn())
return true;
else {
this.router.navigate(['/dashboard/login']);
this.notificationService.notify(NotificationType.ERROR, `You need to log in to access this page`);
return false;
}
}
}