From 425cf197626a93752642a335f5954bca5d66838e Mon Sep 17 00:00:00 2001 From: "Yassine R." Date: Fri, 16 May 2025 01:41:38 +0200 Subject: [PATCH] fix(backend): fix user creation --- .../register-user-admin.component.spec.ts | 8 +++-- .../register-user-admin.component.ts | 9 +++-- .../src/app/guards/auth-guard.spec.ts | 4 +-- .../src/app/guards/auth-guard.ts | 16 ++++----- .../interceptors/server-error.interceptor.ts | 4 +-- .../admin-auth/services/admin-auth.service.ts | 33 ++++++++++--------- 6 files changed, 39 insertions(+), 35 deletions(-) diff --git a/packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.spec.ts b/packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.spec.ts index 5e1c21fdf8..c958517b30 100644 --- a/packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.spec.ts +++ b/packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.spec.ts @@ -7,8 +7,10 @@ import { ReactiveFormsModule, FormsModule } from "@angular/forms"; import { HttpClientTestingModule } from "@angular/common/http/testing"; import { APP_BASE_HREF } from "@angular/common"; -import { CUSTOM_ELEMENTS_SCHEMA } from "@angular/core"; +import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from "@angular/core"; import { RouterTestingModule } from "@angular/router/testing"; +import { StoreModule } from "@ngrx/store"; +import { _usagerReducer } from "../../../../shared"; describe("RegisterUserAdminComponent", () => { let component: RegisterUserAdminComponent; @@ -23,15 +25,17 @@ describe("RegisterUserAdminComponent", () => { FormsModule, HttpClientTestingModule, RouterTestingModule, + StoreModule.forRoot({ app: _usagerReducer }), ], providers: [{ provide: APP_BASE_HREF, useValue: "/" }], - schemas: [CUSTOM_ELEMENTS_SCHEMA], + schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA], }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(RegisterUserAdminComponent); component = fixture.componentInstance; + fixture.detectChanges(); }); diff --git a/packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.ts b/packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.ts index c5104f599b..88c357de5f 100644 --- a/packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.ts +++ b/packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.ts @@ -23,7 +23,7 @@ import { fadeInOut, NoWhiteSpaceValidator, } from "../../../../shared"; -import { CustomToastService } from "../../../shared/services"; +import { AuthService, CustomToastService } from "../../../shared/services"; import { UserStructure } from "@domifa/common"; import { UsersService, userStructureBuilder } from "../../../users/services"; @@ -57,7 +57,8 @@ export class RegisterUserAdminComponent implements OnInit, OnDestroy { constructor( private readonly formBuilder: UntypedFormBuilder, private readonly usersService: UsersService, - private readonly toastService: CustomToastService + private readonly toastService: CustomToastService, + private readonly authService: AuthService ) { this.user = userStructureBuilder.buildUserStructure({}); this.loading = false; @@ -65,6 +66,8 @@ export class RegisterUserAdminComponent implements OnInit, OnDestroy { } public ngOnInit(): void { + const user = this.authService.currentUserValue; + this.userForm = this.formBuilder.group({ email: [ this.user.email, @@ -80,7 +83,7 @@ export class RegisterUserAdminComponent implements OnInit, OnDestroy { this.user.prenom, [Validators.required, Validators.minLength(2), NoWhiteSpaceValidator], ], - structureId: [this.user.structureId, []], + structureId: [user?.structureId, []], }); } diff --git a/packages/portail-admins/src/app/guards/auth-guard.spec.ts b/packages/portail-admins/src/app/guards/auth-guard.spec.ts index b9e2db3088..c50a3c4649 100644 --- a/packages/portail-admins/src/app/guards/auth-guard.spec.ts +++ b/packages/portail-admins/src/app/guards/auth-guard.spec.ts @@ -16,7 +16,6 @@ describe("AuthGuard", () => { let authService: AdminAuthService; let router: Router; - let routerService: RouterStateSnapshot; let toastService: CustomToastService; beforeEach(() => { TestBed.configureTestingModule({ @@ -44,7 +43,6 @@ describe("AuthGuard", () => { router = TestBed.inject(Router); toastService = TestBed.inject(CustomToastService); authGuard = TestBed.inject(AuthGuard); - routerService = TestBed.inject(RouterStateSnapshot); }); it("should be created", inject([AuthGuard], (service: AuthGuard) => { @@ -52,7 +50,7 @@ describe("AuthGuard", () => { })); it("CanActivate", () => { - authGuard = new AuthGuard(authService, router, toastService, routerService); + authGuard = new AuthGuard(authService, router, toastService); expect(authGuard).toBeTruthy(); }); }); diff --git a/packages/portail-admins/src/app/guards/auth-guard.ts b/packages/portail-admins/src/app/guards/auth-guard.ts index a568c05e10..b643ab3713 100644 --- a/packages/portail-admins/src/app/guards/auth-guard.ts +++ b/packages/portail-admins/src/app/guards/auth-guard.ts @@ -2,11 +2,7 @@ import { Injectable } from "@angular/core"; import { Observable, of } from "rxjs"; import { catchError, map } from "rxjs/operators"; import { AdminAuthService } from "../modules/admin-auth/services/admin-auth.service"; -import { - Router, - ActivatedRouteSnapshot, - RouterStateSnapshot, -} from "@angular/router"; +import { Router, ActivatedRouteSnapshot } from "@angular/router"; import { UserSupervisorRole } from "@domifa/common"; import { CustomToastService } from "../modules/shared/services"; @Injectable({ providedIn: "root" }) @@ -14,8 +10,7 @@ export class AuthGuard { constructor( private readonly authService: AdminAuthService, private readonly router: Router, - private readonly toastService: CustomToastService, - private readonly state: RouterStateSnapshot + private readonly toastService: CustomToastService ) {} public canActivate(route: ActivatedRouteSnapshot): Observable { @@ -24,7 +19,11 @@ export class AuthGuard { return this.authService.isAuth().pipe( map((isAuth: boolean) => { if (!isAuth) { - this.authService.logoutAndRedirect(this.state); + const redirectToAfterLogin = + window.location.pathname + window.location.search; + this.authService.logoutAndRedirect({ + redirectToAfterLogin, + }); return false; } @@ -48,7 +47,6 @@ export class AuthGuard { return false; }), catchError(() => { - this.authService.logoutAndRedirect(this.state); return of(false); }) ); diff --git a/packages/portail-admins/src/app/interceptors/server-error.interceptor.ts b/packages/portail-admins/src/app/interceptors/server-error.interceptor.ts index 3bd3b5f604..e2a70c9908 100644 --- a/packages/portail-admins/src/app/interceptors/server-error.interceptor.ts +++ b/packages/portail-admins/src/app/interceptors/server-error.interceptor.ts @@ -13,7 +13,6 @@ import { catchError, retry } from "rxjs/operators"; import { getCurrentScope } from "@sentry/angular"; import { CustomToastService } from "../modules/shared/services"; import { AdminAuthService } from "../modules/admin-auth/services/admin-auth.service"; -import { RouterStateSnapshot } from "@angular/router"; const MAX_RETRIES = 2; const RETRY_DELAY = 1000; @@ -31,7 +30,6 @@ export class ServerErrorInterceptor implements HttpInterceptor { ): Observable> { const authService = this.injector.get(AdminAuthService); const toastr = this.injector.get(CustomToastService); - const state = this.injector.get(RouterStateSnapshot); if (authService?.currentUserValue) { const user = authService.currentUserValue; @@ -70,7 +68,7 @@ export class ServerErrorInterceptor implements HttpInterceptor { ); } if (error.status === 401) { - authService.logoutAndRedirect(state); + authService.logoutAndRedirect(); toastr.error( "Votre session a expiré, merci de vous connecter à nouveau" ); diff --git a/packages/portail-admins/src/app/modules/admin-auth/services/admin-auth.service.ts b/packages/portail-admins/src/app/modules/admin-auth/services/admin-auth.service.ts index e5acba27c6..794cff34e5 100644 --- a/packages/portail-admins/src/app/modules/admin-auth/services/admin-auth.service.ts +++ b/packages/portail-admins/src/app/modules/admin-auth/services/admin-auth.service.ts @@ -1,6 +1,6 @@ import { HttpClient } from "@angular/common/http"; import { Injectable } from "@angular/core"; -import { Router, RouterStateSnapshot } from "@angular/router"; +import { Router } from "@angular/router"; import { BehaviorSubject, catchError, map, Observable, of } from "rxjs"; import { environment } from "../../../../environments/environment"; @@ -75,22 +75,25 @@ export class AdminAuthService { getCurrentScope().setUser({}); } - public logoutAndRedirect( - state?: RouterStateSnapshot, - sessionExpired?: true - ): void { + public logoutAndRedirect({ + redirectToAfterLogin, + }: { + redirectToAfterLogin?: string; + } = {}): void { this.logout(); - if (sessionExpired) { - this.toastr.warning("Votre session a expiré, merci de vous reconnecter"); - } - this.logout(); - if (state) { - this.router.navigate(["/auth/login"], { - queryParams: { returnUrl: state.url }, - }); - } else { - this.router.navigate(["/auth/login"]); + + if (redirectToAfterLogin) { + const cleanPath = redirectToAfterLogin.split("?")[0]; + + if (cleanPath !== "/auth/login") { + this.router.navigate(["/auth/login"], { + queryParams: { redirectToAfterLogin: cleanPath }, + }); + return; + } } + + this.router.navigate(["/auth/login"]); } public notAuthorized(): void {