diff --git a/frontend/src/app/modules/login/login/login.component.ts b/frontend/src/app/modules/login/login/login.component.ts index 691a6d410c..6b6f9ac4af 100644 --- a/frontend/src/app/modules/login/login/login.component.ts +++ b/frontend/src/app/modules/login/login/login.component.ts @@ -48,13 +48,13 @@ export class LoginComponent implements OnInit { } ngOnInit() { - if (this.config.CAS_PUBLIC.CAS_AUTHENTIFICATION) { - // if token not here here, redirection to CAS login page - const url_redirection_cas = `${this.config.CAS_PUBLIC.CAS_URL_LOGIN}?service=${this.config.API_ENDPOINT}/gn_auth/login_cas`; - if (!this._authService.isLoggedIn()) { - document.location.href = url_redirection_cas; - } - } + // if (this.config.CAS_PUBLIC.CAS_AUTHENTIFICATION) { + // // if token not here here, redirection to CAS login page + // const url_redirection_cas = `${this.config.CAS_PUBLIC.CAS_URL_LOGIN}?service=${this.config.API_ENDPOINT}/gn_auth/login_cas`; + // if (!this._authService.isLoggedIn()) { + // document.location.href = url_redirection_cas; + // } + // } } async register(user) { diff --git a/frontend/src/app/routing/app-routing.module.ts b/frontend/src/app/routing/app-routing.module.ts index d29f5b5c22..398851f52c 100644 --- a/frontend/src/app/routing/app-routing.module.ts +++ b/frontend/src/app/routing/app-routing.module.ts @@ -40,7 +40,7 @@ const defaultRoutes: Routes = [ { path: '', component: NavHomeComponent, - canActivate: [UserCasGuard], + // canActivate: [UserCasGuard], canActivateChild: [AuthGuard], children: [ { diff --git a/frontend/src/app/routing/auth-guard.service.ts b/frontend/src/app/routing/auth-guard.service.ts index a5d3db3406..14a159372f 100644 --- a/frontend/src/app/routing/auth-guard.service.ts +++ b/frontend/src/app/routing/auth-guard.service.ts @@ -6,6 +6,8 @@ import { RouterStateSnapshot, } from '@angular/router'; import { Injectable, Injector } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; + import { AuthService } from '@geonature/components/auth/auth.service'; import { ModuleService } from '@geonature/services/module.service'; import { ConfigService } from '@geonature/services/config.service'; @@ -15,7 +17,10 @@ import { RoutingService } from './routing.service'; export class AuthGuard implements CanActivate, CanActivateChild { constructor( private _router: Router, - private _injector: Injector + private _injector: Injector, + private _httpclient: HttpClient, + private config: ConfigService, + private _authService: AuthService ) {} async redirectAuth(route, state) { @@ -38,12 +43,20 @@ export class AuthGuard implements CanActivate, CanActivateChild { return false; }); if (data) { - await authService.manageUser(data).toPromise(); + authService.manageUser(data); const modules = await moduleService.loadModules().toPromise(); - routingService.loadRoutes(modules, route._routerState.url); + routingService.loadRoutes(modules, this.getNewURLWithoutAccessPublic(route)); } else { return false; } + } else if (configService.CAS_PUBLIC.CAS_AUTHENTIFICATION) { + const url_redirection_cas = `${configService.CAS_PUBLIC.CAS_URL_LOGIN}?service=${configService.API_ENDPOINT}/gn_auth/login_cas`; + document.location.href = url_redirection_cas; + let data = await this._httpclient + .get(`${this.config.API_ENDPOINT}/auth/get_current_user`) + .toPromise(); + data = { ...data }; + this._authService.manageUser(data); } else { this._router.navigate(['/login'], { queryParams: { ...route.queryParams, ...{ route: state.url.split('?')[0] } }, @@ -64,4 +77,15 @@ export class AuthGuard implements CanActivate, CanActivateChild { canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { return this.redirectAuth(route, state); } + + getNewURLWithoutAccessPublic(route) { + const queryParams = { ...route.queryParams }; + delete queryParams.access; + const path = route.url[0] ? route.url[0].path : ''; + const urlTree = this._router.createUrlTree([path], { + queryParams, + queryParamsHandling: 'merge', + }); + return this._router.serializeUrl(urlTree); + } }