Skip to content

Commit 425cf19

Browse files
committed
fix(backend): fix user creation
1 parent 85864bf commit 425cf19

File tree

6 files changed

+39
-35
lines changed

6 files changed

+39
-35
lines changed

packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { ReactiveFormsModule, FormsModule } from "@angular/forms";
77
import { HttpClientTestingModule } from "@angular/common/http/testing";
88

99
import { APP_BASE_HREF } from "@angular/common";
10-
import { CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
10+
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from "@angular/core";
1111
import { RouterTestingModule } from "@angular/router/testing";
12+
import { StoreModule } from "@ngrx/store";
13+
import { _usagerReducer } from "../../../../shared";
1214

1315
describe("RegisterUserAdminComponent", () => {
1416
let component: RegisterUserAdminComponent;
@@ -23,15 +25,17 @@ describe("RegisterUserAdminComponent", () => {
2325
FormsModule,
2426
HttpClientTestingModule,
2527
RouterTestingModule,
28+
StoreModule.forRoot({ app: _usagerReducer }),
2629
],
2730
providers: [{ provide: APP_BASE_HREF, useValue: "/" }],
28-
schemas: [CUSTOM_ELEMENTS_SCHEMA],
31+
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
2932
}).compileComponents();
3033
}));
3134

3235
beforeEach(() => {
3336
fixture = TestBed.createComponent(RegisterUserAdminComponent);
3437
component = fixture.componentInstance;
38+
3539
fixture.detectChanges();
3640
});
3741

packages/frontend/src/app/modules/manage-users/components/register-user-admin/register-user-admin.component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
fadeInOut,
2424
NoWhiteSpaceValidator,
2525
} from "../../../../shared";
26-
import { CustomToastService } from "../../../shared/services";
26+
import { AuthService, CustomToastService } from "../../../shared/services";
2727

2828
import { UserStructure } from "@domifa/common";
2929
import { UsersService, userStructureBuilder } from "../../../users/services";
@@ -57,14 +57,17 @@ export class RegisterUserAdminComponent implements OnInit, OnDestroy {
5757
constructor(
5858
private readonly formBuilder: UntypedFormBuilder,
5959
private readonly usersService: UsersService,
60-
private readonly toastService: CustomToastService
60+
private readonly toastService: CustomToastService,
61+
private readonly authService: AuthService
6162
) {
6263
this.user = userStructureBuilder.buildUserStructure({});
6364
this.loading = false;
6465
this.submitted = false;
6566
}
6667

6768
public ngOnInit(): void {
69+
const user = this.authService.currentUserValue;
70+
6871
this.userForm = this.formBuilder.group({
6972
email: [
7073
this.user.email,
@@ -80,7 +83,7 @@ export class RegisterUserAdminComponent implements OnInit, OnDestroy {
8083
this.user.prenom,
8184
[Validators.required, Validators.minLength(2), NoWhiteSpaceValidator],
8285
],
83-
structureId: [this.user.structureId, []],
86+
structureId: [user?.structureId, []],
8487
});
8588
}
8689

packages/portail-admins/src/app/guards/auth-guard.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe("AuthGuard", () => {
1616

1717
let authService: AdminAuthService;
1818
let router: Router;
19-
let routerService: RouterStateSnapshot;
2019
let toastService: CustomToastService;
2120
beforeEach(() => {
2221
TestBed.configureTestingModule({
@@ -44,15 +43,14 @@ describe("AuthGuard", () => {
4443
router = TestBed.inject(Router);
4544
toastService = TestBed.inject(CustomToastService);
4645
authGuard = TestBed.inject(AuthGuard);
47-
routerService = TestBed.inject(RouterStateSnapshot);
4846
});
4947

5048
it("should be created", inject([AuthGuard], (service: AuthGuard) => {
5149
expect(service).toBeTruthy();
5250
}));
5351

5452
it("CanActivate", () => {
55-
authGuard = new AuthGuard(authService, router, toastService, routerService);
53+
authGuard = new AuthGuard(authService, router, toastService);
5654
expect(authGuard).toBeTruthy();
5755
});
5856
});

packages/portail-admins/src/app/guards/auth-guard.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@ import { Injectable } from "@angular/core";
22
import { Observable, of } from "rxjs";
33
import { catchError, map } from "rxjs/operators";
44
import { AdminAuthService } from "../modules/admin-auth/services/admin-auth.service";
5-
import {
6-
Router,
7-
ActivatedRouteSnapshot,
8-
RouterStateSnapshot,
9-
} from "@angular/router";
5+
import { Router, ActivatedRouteSnapshot } from "@angular/router";
106
import { UserSupervisorRole } from "@domifa/common";
117
import { CustomToastService } from "../modules/shared/services";
128
@Injectable({ providedIn: "root" })
139
export class AuthGuard {
1410
constructor(
1511
private readonly authService: AdminAuthService,
1612
private readonly router: Router,
17-
private readonly toastService: CustomToastService,
18-
private readonly state: RouterStateSnapshot
13+
private readonly toastService: CustomToastService
1914
) {}
2015

2116
public canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
@@ -24,7 +19,11 @@ export class AuthGuard {
2419
return this.authService.isAuth().pipe(
2520
map((isAuth: boolean) => {
2621
if (!isAuth) {
27-
this.authService.logoutAndRedirect(this.state);
22+
const redirectToAfterLogin =
23+
window.location.pathname + window.location.search;
24+
this.authService.logoutAndRedirect({
25+
redirectToAfterLogin,
26+
});
2827
return false;
2928
}
3029

@@ -48,7 +47,6 @@ export class AuthGuard {
4847
return false;
4948
}),
5049
catchError(() => {
51-
this.authService.logoutAndRedirect(this.state);
5250
return of(false);
5351
})
5452
);

packages/portail-admins/src/app/interceptors/server-error.interceptor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { catchError, retry } from "rxjs/operators";
1313
import { getCurrentScope } from "@sentry/angular";
1414
import { CustomToastService } from "../modules/shared/services";
1515
import { AdminAuthService } from "../modules/admin-auth/services/admin-auth.service";
16-
import { RouterStateSnapshot } from "@angular/router";
1716

1817
const MAX_RETRIES = 2;
1918
const RETRY_DELAY = 1000;
@@ -31,7 +30,6 @@ export class ServerErrorInterceptor implements HttpInterceptor {
3130
): Observable<HttpEvent<any>> {
3231
const authService = this.injector.get(AdminAuthService);
3332
const toastr = this.injector.get(CustomToastService);
34-
const state = this.injector.get(RouterStateSnapshot);
3533

3634
if (authService?.currentUserValue) {
3735
const user = authService.currentUserValue;
@@ -70,7 +68,7 @@ export class ServerErrorInterceptor implements HttpInterceptor {
7068
);
7169
}
7270
if (error.status === 401) {
73-
authService.logoutAndRedirect(state);
71+
authService.logoutAndRedirect();
7472
toastr.error(
7573
"Votre session a expiré, merci de vous connecter à nouveau"
7674
);

packages/portail-admins/src/app/modules/admin-auth/services/admin-auth.service.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HttpClient } from "@angular/common/http";
22
import { Injectable } from "@angular/core";
3-
import { Router, RouterStateSnapshot } from "@angular/router";
3+
import { Router } from "@angular/router";
44

55
import { BehaviorSubject, catchError, map, Observable, of } from "rxjs";
66
import { environment } from "../../../../environments/environment";
@@ -75,22 +75,25 @@ export class AdminAuthService {
7575
getCurrentScope().setUser({});
7676
}
7777

78-
public logoutAndRedirect(
79-
state?: RouterStateSnapshot,
80-
sessionExpired?: true
81-
): void {
78+
public logoutAndRedirect({
79+
redirectToAfterLogin,
80+
}: {
81+
redirectToAfterLogin?: string;
82+
} = {}): void {
8283
this.logout();
83-
if (sessionExpired) {
84-
this.toastr.warning("Votre session a expiré, merci de vous reconnecter");
85-
}
86-
this.logout();
87-
if (state) {
88-
this.router.navigate(["/auth/login"], {
89-
queryParams: { returnUrl: state.url },
90-
});
91-
} else {
92-
this.router.navigate(["/auth/login"]);
84+
85+
if (redirectToAfterLogin) {
86+
const cleanPath = redirectToAfterLogin.split("?")[0];
87+
88+
if (cleanPath !== "/auth/login") {
89+
this.router.navigate(["/auth/login"], {
90+
queryParams: { redirectToAfterLogin: cleanPath },
91+
});
92+
return;
93+
}
9394
}
95+
96+
this.router.navigate(["/auth/login"]);
9497
}
9598

9699
public notAuthorized(): void {

0 commit comments

Comments
 (0)