Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 71c657c

Browse files
committed
✨ mis a jour des guards avec la gestion du token et ajout de l'interceptor
1 parent 737393c commit 71c657c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/app/app.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { APP_INITIALIZER, ErrorHandler, NgModule } from '@angular/core';
2-
import { HttpClientModule } from '@angular/common/http';
2+
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
33
import { BrowserModule } from '@angular/platform-browser';
44
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
55
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@@ -14,6 +14,7 @@ import { AppRoutingModule } from './core/routes/app-routing.module';
1414
import { AppComponent } from './core/components/app/app.component';
1515
import { ROOT_REDUCERS } from './core/store/app.store';
1616
import { Router } from '@angular/router';
17+
import { AuthInterceptor } from './modules/authentication/interceptors/auth.interceptor';
1718

1819
@NgModule({
1920
declarations: [
@@ -48,6 +49,11 @@ import { Router } from '@angular/router';
4849
deps: [Sentry.TraceService],
4950
multi: true,
5051
},
52+
{
53+
provide: HTTP_INTERCEPTORS,
54+
useClass: AuthInterceptor,
55+
multi: true,
56+
}
5157
],
5258
bootstrap: [AppComponent],
5359
})

src/app/core/guards/auth.guard.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { CanActivateChild, Router } from '@angular/router';
3+
import { AccessTokenService } from '@app/modules/authentication/services/access-token.service';
34
import { selectIsLoggedIn } from '@app/modules/authentication/store/auth.selectors';
45
import { Store } from '@ngrx/store';
56
import { filter, first, map, Observable } from 'rxjs';
@@ -8,13 +9,17 @@ import { filter, first, map, Observable } from 'rxjs';
89
providedIn: 'root'
910
})
1011
export class AuthGuard implements CanActivateChild {
11-
constructor(private store: Store, private router: Router) {}
12+
constructor(
13+
private store: Store,
14+
private router: Router,
15+
private accessTokenService: AccessTokenService
16+
) {}
1217

1318
canActivateChild(): Observable<boolean> {
1419
return this.store.select(selectIsLoggedIn).pipe(
1520
first((value) => value !== null),
1621
map((isLoggedIn: boolean | null) => {
17-
if (! isLoggedIn) {
22+
if (! isLoggedIn && ! this.accessTokenService.getAccessToken()) {
1823
this.router.navigateByUrl('/auth/login')
1924
return false;
2025
}

src/app/core/guards/guest.guard.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { ActivatedRouteSnapshot, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router';
3+
import { AccessTokenService } from '@app/modules/authentication/services/access-token.service';
34
import { selectIsLoggedIn } from '@app/modules/authentication/store/auth.selectors';
45
import { Store } from '@ngrx/store';
56
import { filter, first, map, Observable } from 'rxjs';
@@ -8,13 +9,17 @@ import { filter, first, map, Observable } from 'rxjs';
89
providedIn: 'root'
910
})
1011
export class GuestGuard implements CanActivateChild {
11-
constructor(private store: Store, private router: Router) {}
12+
constructor(
13+
private store: Store,
14+
private router: Router,
15+
private accessTokenService: AccessTokenService
16+
) {}
1217

1318
canActivateChild(): Observable<boolean> {
1419
return this.store.select(selectIsLoggedIn).pipe(
1520
first((value) => value !== null),
1621
map((isLoggedIn: boolean | null) => {
17-
if (isLoggedIn) {
22+
if (isLoggedIn && this.accessTokenService.getAccessToken()) {
1823
this.router.navigateByUrl('/dashboard')
1924
return false
2025
}

0 commit comments

Comments
 (0)