openaire-library/login/loginGuard.guard.ts

48 lines
1.5 KiB
TypeScript

import {Injectable} from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Route,
CanLoad,
UrlSegment
} from '@angular/router';
import {Observable} from 'rxjs';
import {Session} from './utils/helper.class';
import {LoginErrorCodes} from './utils/guardHelper.class';
import {filter, map, mergeMap} from "rxjs/operators";
import {CommunityService} from "../connect/community/community.service";
import {UserManagementService} from "../services/user-management.service";
import {EnvironmentSpecificService} from "../utils/properties/environment-specific.service";
@Injectable()
export class LoginGuard implements CanActivate {
constructor(private router: Router,
private userManagementService: UserManagementService,
private propertiesService: EnvironmentSpecificService) {
}
check(path: string): Observable<boolean> |boolean {
const loggedIn = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
return this.userManagementService.isLoggedIn(properties['userInfoUrl']).pipe(map( isLoggedIn => {
return isLoggedIn;
}));
}));
loggedIn.pipe(filter( isLoggedIn => !isLoggedIn)).subscribe( () => {
this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': LoginErrorCodes.NOT_LOGIN,
'redirectUrl': path
}
});
});
return loggedIn;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(state.url);
}
}