openaire-library/connect/communityGuard/connectSubscriber.guard.ts

34 lines
1.2 KiB
TypeScript

import {take, tap} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
import {SubscribeService} from "../../utils/subscribe/subscribe.service";
import {properties} from "../../../../environments/environment";
@Injectable()
export class ConnectSubscriberGuard implements CanActivate {
constructor(private router: Router,
private subscribeService: SubscribeService) {
}
check(community: string, path: string): Observable<boolean> {
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
return this.subscribeService.isSubscribedToCommunity(properties, community).pipe(take(1), tap(subscribed =>{
if(!subscribed){
this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': errorCode,
'redirectUrl': path
}
});
}
}));
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId'], state.url);
}
}