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

Commit dd138ec

Browse files
committed
♻️ mis a jour des guard et optimization de code
1 parent 31cdd74 commit dd138ec

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

src/app/app.module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { environment } from '../environments/environment';
1313
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';
16-
import { ROOT_EFFECTS } from './core/store/app.effects';
1716
import { Router } from '@angular/router';
1817

1918
@NgModule({
@@ -24,7 +23,7 @@ import { Router } from '@angular/router';
2423
AppRoutingModule,
2524
BrowserAnimationsModule,
2625
BrowserModule,
27-
EffectsModule.forRoot(ROOT_EFFECTS),
26+
EffectsModule.forRoot(),
2827
FormsModule,
2928
HttpClientModule,
3029
ReactiveFormsModule,

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
import { Injectable } from '@angular/core';
22
import { CanActivate } from '@angular/router';
3+
import { Store } from '@ngrx/store';
4+
import { first, map, Observable } from 'rxjs';
5+
6+
import { getCurrentUserAction } from '@app/modules/authentication/store/auth.actions';
7+
import { selectCurrentUser } from '@app/modules/authentication/store/auth.selectors';
8+
import { User } from '@app/modules/user/interfaces/user.interface';
39

410
@Injectable({
511
providedIn: 'root'
612
})
713
export class AdminGuard implements CanActivate {
814

15+
constructor(private store: Store) {}
16+
917
emails: string[] = [
1018
1119
1220
1321
];
1422

15-
canActivate(): boolean {
16-
return true;
23+
canActivate(): Observable<boolean> {
24+
return this.store.select(selectCurrentUser).pipe(
25+
first(),
26+
map((user: User | null) => {
27+
if (!user) {
28+
this.store.dispatch(getCurrentUserAction());
29+
return false;
30+
}
31+
return this.emails.includes(user.email);
32+
})
33+
);
1734
}
1835

1936
}

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import { Injectable } from '@angular/core';
2-
import { ActivatedRouteSnapshot, CanActivateChild, RouterStateSnapshot } from '@angular/router';
2+
import { CanActivateChild, Router } from '@angular/router';
3+
import { selectIsLoggedIn } from '@app/modules/authentication/store/auth.selectors';
4+
import { Store } from '@ngrx/store';
5+
import { filter, first, map, Observable } from 'rxjs';
36

47
@Injectable({
58
providedIn: 'root'
69
})
710
export class AuthGuard implements CanActivateChild {
11+
constructor(private store: Store, private router: Router) {}
812

9-
canActivateChild(
10-
childRoute: ActivatedRouteSnapshot,
11-
state: RouterStateSnapshot
12-
): boolean {
13-
return true;
13+
canActivateChild(): Observable<boolean> {
14+
return this.store.select(selectIsLoggedIn).pipe(
15+
first((value) => value !== null),
16+
map((isLoggedIn: boolean | null) => {
17+
if (! isLoggedIn) {
18+
this.router.navigateByUrl('/auth/login')
19+
return false;
20+
}
21+
22+
return true;
23+
})
24+
);
1425
}
1526

1627
}

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import { Injectable } from '@angular/core';
2-
import { ActivatedRouteSnapshot, CanActivateChild, RouterStateSnapshot } from '@angular/router';
2+
import { ActivatedRouteSnapshot, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router';
3+
import { selectIsLoggedIn } from '@app/modules/authentication/store/auth.selectors';
4+
import { Store } from '@ngrx/store';
5+
import { filter, first, map, Observable } from 'rxjs';
36

47
@Injectable({
58
providedIn: 'root'
69
})
710
export class GuestGuard implements CanActivateChild {
11+
constructor(private store: Store, private router: Router) {}
812

9-
canActivateChild(
10-
route: ActivatedRouteSnapshot,
11-
state: RouterStateSnapshot
12-
): boolean {
13-
return true;
13+
canActivateChild(): Observable<boolean> {
14+
return this.store.select(selectIsLoggedIn).pipe(
15+
first((value) => value !== null),
16+
map((isLoggedIn: boolean | null) => {
17+
if (isLoggedIn) {
18+
this.router.navigateByUrl('/dashboard')
19+
return false
20+
}
21+
22+
return true;
23+
})
24+
);
1425
}
1526

1627
}

src/app/core/store/app.effects.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/app/core/store/app.store.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { AuthState } from '@app/modules/authentication/interfaces/state.interface';
2+
import { authFeatureKey, authReducer } from '@app/modules/authentication/store/auth.reducer';
13
import { routerReducer, RouterState } from '@ngrx/router-store';
24
import { Action, ActionReducerMap } from '@ngrx/store';
35

46
export interface AppState {
57
router: RouterState;
8+
[authFeatureKey]: AuthState;
69
}
710

811
export const ROOT_REDUCERS: ActionReducerMap<AppState, Action> = {
912
router: routerReducer,
13+
[authFeatureKey]: authReducer,
1014
};

0 commit comments

Comments
 (0)