2019-09-13 12:02:30 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-03-01 19:19:53 +01:00
|
|
|
import {
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
CanActivate,
|
|
|
|
CanActivateChild,
|
|
|
|
CanLoad,
|
|
|
|
Route,
|
|
|
|
Router,
|
2023-06-26 10:31:22 +02:00
|
|
|
RouterStateSnapshot,
|
|
|
|
UrlTree
|
2021-03-01 19:19:53 +01:00
|
|
|
} from '@angular/router';
|
2021-07-14 13:19:57 +02:00
|
|
|
import {Observable} from 'rxjs';
|
2018-11-01 18:20:05 +01:00
|
|
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
2020-11-11 15:43:13 +01:00
|
|
|
import {map, tap} from "rxjs/operators";
|
2020-01-29 11:20:53 +01:00
|
|
|
import {UserManagementService} from "../services/user-management.service";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2023-06-26 10:31:22 +02:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2021-03-01 19:19:53 +01:00
|
|
|
export class LoginGuard implements CanActivate, CanLoad, CanActivateChild {
|
2020-01-29 11:20:53 +01:00
|
|
|
|
|
|
|
constructor(private router: Router,
|
|
|
|
private userManagementService: UserManagementService) {
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
|
|
|
check(path: string): Observable<boolean> | boolean {
|
2022-09-08 15:43:59 +02:00
|
|
|
return this.userManagementService.getUserInfo().pipe(map(user => {
|
2022-04-13 12:50:55 +02:00
|
|
|
return user !== null;
|
|
|
|
}),tap(isLoggedIn => {
|
|
|
|
if(!isLoggedIn) {
|
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': LoginErrorCodes.NOT_LOGIN,
|
|
|
|
'redirectUrl': path
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}));
|
2019-01-25 12:18:38 +01:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
2021-07-14 13:19:57 +02:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
2019-01-29 14:03:31 +01:00
|
|
|
return this.check(state.url);
|
2019-01-25 12:18:38 +01:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
2021-03-01 19:19:53 +01:00
|
|
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
|
|
|
return this.check(state.url);
|
|
|
|
}
|
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
|
|
|
|
return this.check('/' + route.path);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|