Skip to content

Instantly share code, notes, and snippets.

@blakazulu
Created January 25, 2021 14:23
Show Gist options
  • Save blakazulu/19ac485118678502496e0b8218273bde to your computer and use it in GitHub Desktop.
Save blakazulu/19ac485118678502496e0b8218273bde to your computer and use it in GitHub Desktop.
Angular CMS
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