Created
February 27, 2020 12:11
-
-
Save umutyerebakmaz/3db2152c00de48da73c2fa5e9246e410 to your computer and use it in GitHub Desktop.
Rollere dayalı route koruması.
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 { Injectable } from '@angular/core'; | |
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; | |
import { LoginMeGQL } from '@generated-types'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class AuthRouteGuard implements CanActivate { | |
constructor( | |
private router: Router, | |
private loginMeGQL: LoginMeGQL | |
) { } | |
async canActivate( | |
router: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot | |
) { | |
const result = await this.loginMeGQL.fetch({}, { | |
fetchPolicy: 'cache-first' | |
}).toPromise(); | |
if (result.data.me) { | |
if (router.data.roles && router.data.roles.indexOf(result.data.me.role) === -1) { | |
this.router.navigateByUrl('/'); | |
return false; | |
} | |
return true; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment