Skip to content

Commit 3ab3adf

Browse files
committed
fix(backend): fix user creation
1 parent 1219af8 commit 3ab3adf

File tree

7 files changed

+45
-14
lines changed

7 files changed

+45
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class RegisterUserAdminComponent implements OnInit, OnDestroy {
8080
this.user.prenom,
8181
[Validators.required, Validators.minLength(2), NoWhiteSpaceValidator],
8282
],
83+
structureId: [this.user.structureId, []],
8384
});
8485
}
8586

packages/frontend/src/app/modules/users/services/users.service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HttpClient } from "@angular/common/http";
22
import { Injectable } from "@angular/core";
33
import { Observable } from "rxjs";
44
import { environment } from "src/environments/environment";
5-
import { ApiMessage } from "@domifa/common";
5+
import { ApiMessage, UserStructure } from "@domifa/common";
66

77
@Injectable({
88
providedIn: "root",
@@ -43,7 +43,12 @@ export class UsersService {
4343
return this.http.post(`${this.endPoint}/reset-password`, data);
4444
}
4545

46-
public registerUser(data: string): Observable<ApiMessage> {
46+
public registerUser(
47+
data: Pick<
48+
UserStructure,
49+
"email" | "nom" | "role" | "prenom" | "structureId"
50+
>
51+
): Observable<ApiMessage> {
4752
return this.http.post<ApiMessage>(`${this.endPoint}/register`, data);
4853
}
4954
}

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

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

1717
let authService: AdminAuthService;
1818
let router: Router;
19+
let routerService: RouterStateSnapshot;
1920
let toastService: CustomToastService;
2021
beforeEach(() => {
2122
TestBed.configureTestingModule({
@@ -43,14 +44,15 @@ describe("AuthGuard", () => {
4344
router = TestBed.inject(Router);
4445
toastService = TestBed.inject(CustomToastService);
4546
authGuard = TestBed.inject(AuthGuard);
47+
routerService = TestBed.inject(RouterStateSnapshot);
4648
});
4749

4850
it("should be created", inject([AuthGuard], (service: AuthGuard) => {
4951
expect(service).toBeTruthy();
5052
}));
5153

5254
it("CanActivate", () => {
53-
authGuard = new AuthGuard(authService, router, toastService);
55+
authGuard = new AuthGuard(authService, router, toastService, routerService);
5456
expect(authGuard).toBeTruthy();
5557
});
5658
});

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ 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 { Router, ActivatedRouteSnapshot } from "@angular/router";
5+
import {
6+
Router,
7+
ActivatedRouteSnapshot,
8+
RouterStateSnapshot,
9+
} from "@angular/router";
610
import { UserSupervisorRole } from "@domifa/common";
711
import { CustomToastService } from "../modules/shared/services";
812
@Injectable({ providedIn: "root" })
913
export class AuthGuard {
1014
constructor(
1115
private readonly authService: AdminAuthService,
1216
private readonly router: Router,
13-
private readonly toastService: CustomToastService
17+
private readonly toastService: CustomToastService,
18+
private readonly state: RouterStateSnapshot
1419
) {}
1520

1621
public canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
@@ -19,7 +24,7 @@ export class AuthGuard {
1924
return this.authService.isAuth().pipe(
2025
map((isAuth: boolean) => {
2126
if (!isAuth) {
22-
this.authService.logoutAndRedirect();
27+
this.authService.logoutAndRedirect(this.state);
2328
return false;
2429
}
2530

@@ -43,7 +48,7 @@ export class AuthGuard {
4348
return false;
4449
}),
4550
catchError(() => {
46-
this.authService.logoutAndRedirect();
51+
this.authService.logoutAndRedirect(this.state);
4752
return of(false);
4853
})
4954
);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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";
1617

1718
const MAX_RETRIES = 2;
1819
const RETRY_DELAY = 1000;
@@ -30,6 +31,7 @@ export class ServerErrorInterceptor implements HttpInterceptor {
3031
): Observable<HttpEvent<any>> {
3132
const authService = this.injector.get(AdminAuthService);
3233
const toastr = this.injector.get(CustomToastService);
34+
const state = this.injector.get(RouterStateSnapshot);
3335

3436
if (authService?.currentUserValue) {
3537
const user = authService.currentUserValue;
@@ -68,7 +70,7 @@ export class ServerErrorInterceptor implements HttpInterceptor {
6870
);
6971
}
7072
if (error.status === 401) {
71-
authService.logoutAndRedirect();
73+
authService.logoutAndRedirect(state);
7274
toastr.error(
7375
"Votre session a expiré, merci de vous connecter à nouveau"
7476
);

packages/portail-admins/src/app/modules/admin-auth/admin-login/admin-login.component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Validators,
77
} from "@angular/forms";
88
import { Title } from "@angular/platform-browser";
9-
import { Router } from "@angular/router";
9+
import { ActivatedRoute, Router } from "@angular/router";
1010
import { regexp } from "../../../shared/utils/validators";
1111
import { CustomToastService } from "../../shared/services/custom-toast.service";
1212
import { PortailAdminAuthLoginForm } from "../types";
@@ -24,20 +24,23 @@ export class AdminLoginComponent implements OnInit {
2424

2525
public hidePassword: boolean;
2626
public loading: boolean;
27-
27+
public returnUrl: string;
2828
constructor(
2929
private readonly formBuilder: UntypedFormBuilder,
3030
private readonly router: Router,
31+
private readonly route: ActivatedRoute,
3132
private readonly titleService: Title,
3233
private readonly authService: AdminAuthService,
3334
private readonly toastr: CustomToastService
3435
) {
3536
this.hidePassword = true;
3637
this.loading = false;
38+
this.returnUrl = "/";
3739
}
3840

3941
public ngOnInit(): void {
40-
this.titleService.setTitle("Connexion à DomiFa");
42+
this.titleService.setTitle("Connexion à l'administration de DomiFa");
43+
this.returnUrl = this.route.snapshot.queryParams.returnUrl || "/";
4144
this.initForm();
4245
}
4346

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

Lines changed: 16 additions & 3 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 } from "@angular/router";
3+
import { Router, RouterStateSnapshot } from "@angular/router";
44

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

78-
public logoutAndRedirect(): void {
78+
public logoutAndRedirect(
79+
state?: RouterStateSnapshot,
80+
sessionExpired?: true
81+
): void {
7982
this.logout();
80-
this.router.navigate(["/auth/login"]);
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"]);
93+
}
8194
}
8295

8396
public notAuthorized(): void {

0 commit comments

Comments
 (0)