openaire-library/login/loginGuard.guard.ts

41 lines
919 B
TypeScript
Raw Normal View History

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';
@Injectable()
export class LoginGuard implements CanActivate {
constructor(private router: Router) {
}
check(path: string): boolean {
let loggedIn = false;
if (Session.isLoggedIn()) {
loggedIn = true;
}
if (!loggedIn) {
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);
}
}