Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -57,14 +57,17 @@ 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;
this.submitted = false;
}

public ngOnInit(): void {
const user = this.authService.currentUserValue;

this.userForm = this.formBuilder.group({
email: [
this.user.email,
Expand All @@ -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, []],
});
}

Expand Down
4 changes: 1 addition & 3 deletions packages/portail-admins/src/app/guards/auth-guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe("AuthGuard", () => {

let authService: AdminAuthService;
let router: Router;
let routerService: RouterStateSnapshot;
let toastService: CustomToastService;
beforeEach(() => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -44,15 +43,14 @@ 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) => {
expect(service).toBeTruthy();
}));

it("CanActivate", () => {
authGuard = new AuthGuard(authService, router, toastService, routerService);
authGuard = new AuthGuard(authService, router, toastService);
expect(authGuard).toBeTruthy();
});
});
16 changes: 7 additions & 9 deletions packages/portail-admins/src/app/guards/auth-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ 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" })
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<boolean> {
Expand All @@ -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;
}

Expand All @@ -48,7 +47,6 @@ export class AuthGuard {
return false;
}),
catchError(() => {
this.authService.logoutAndRedirect(this.state);
return of(false);
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +30,6 @@ export class ServerErrorInterceptor implements HttpInterceptor {
): Observable<HttpEvent<any>> {
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;
Expand Down Expand Up @@ -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"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 {
Expand Down
Loading