Created
July 26, 2023 09:50
-
-
Save kavin-1103/5676105ba154f3f826084b8454e8447f to your computer and use it in GitHub Desktop.
auth.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CanActivateFn } from '@angular/router'; | |
import { ActivatedRouteSnapshot, RouterStateSnapshot,UrlTree } from '@angular/router'; | |
import { Router } from '@angular/router'; | |
import { inject} from '@angular/core'; | |
import { AuthService } from './auth.service'; | |
export const authguardGuard: CanActivateFn = ( | |
route: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot): boolean | UrlTree => { | |
const auth = inject(AuthService); | |
const router = inject(Router); | |
const requiredRole = route.data['role']; | |
if(auth.IsloggedIn(requiredRole)) | |
{ | |
return true; | |
} | |
else{ | |
router.navigate(['restricted']); | |
return false; | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment