Created
January 25, 2021 14:23
-
-
Save blakazulu/19ac485118678502496e0b8218273bde to your computer and use it in GitHub Desktop.
Angular CMS
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 {ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree} from '@angular/router'; | |
import {Observable} from 'rxjs'; | |
import {AngularFirebaseService} from '../services/angular-firebase.service'; | |
import {map, take, tap} from 'rxjs/operators'; | |
import {User} from '../interfaces/user'; | |
import {ConsoleLoggerService} from '../services/console-logger.service'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class SubscriberGuard implements CanActivate { | |
constructor(private afService: AngularFirebaseService, private logs: ConsoleLoggerService) { | |
} | |
canActivate( | |
route: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { | |
return this.afService.user$ | |
.pipe( | |
take(1), | |
map((user: User) => !!(user && user.roles.subscriber)), | |
tap((isAdmin) => { | |
if (!isAdmin) { | |
this.logs.warn('Access is denied, Subscriber only allowed'); | |
} | |
})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment