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

39 lines
1.4 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";
import {ConnectHelper} from "../connectHelper";
@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;
let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain);
if (communityDomain) {
community = communityDomain;
}
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);
}
}