Skip to content

Commit be23e97

Browse files
authored
Merge pull request #3616 from tarlepp/feat/guard-refactor
Refactored guards to use simpler logic
2 parents 20f9218 + 01f14ae commit be23e97

File tree

8 files changed

+22
-116
lines changed

8 files changed

+22
-116
lines changed

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
1-
import { Injectable, inject } from '@angular/core';
2-
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, UrlTree } from '@angular/router';
33
import { Observable } from 'rxjs';
44

55
import { BaseAuth } from 'src/app/auth/guards/base-auth';
6-
import { AuthenticationService } from 'src/app/auth/services';
76

87
@Injectable({
98
providedIn: 'root',
109
})
1110
export class AnonymousGuard extends BaseAuth {
12-
protected readonly router: Router = inject(Router);
13-
protected readonly authenticationService: AuthenticationService = inject(AuthenticationService);
14-
15-
/**
16-
* Constructor of the class, where we DI all services that we need to use
17-
* within this guard.
18-
*/
19-
public constructor() {
20-
const router: Router = inject(Router);
21-
const authenticationService: AuthenticationService = inject(AuthenticationService);
22-
23-
super(router, authenticationService);
24-
}
25-
2611
/**
2712
* Purpose of this guard is check that current user has not been authenticated
2813
* to application. If user is authenticated he/she is redirected to application

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
1-
import { Injectable, inject } from '@angular/core';
2-
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, UrlTree } from '@angular/router';
33
import { Observable } from 'rxjs';
44

55
import { BaseAuth } from 'src/app/auth/guards/base-auth';
6-
import { AuthenticationService } from 'src/app/auth/services';
76

87
@Injectable({
98
providedIn: 'root',
109
})
1110
export class AuthenticationGuard extends BaseAuth {
12-
protected readonly router: Router = inject(Router);
13-
protected readonly authenticationService: AuthenticationService = inject(AuthenticationService);
14-
15-
/**
16-
* Constructor of the class, where we DI all services that we need to use
17-
* within this guard.
18-
*/
19-
public constructor() {
20-
const router: Router = inject(Router);
21-
const authenticationService: AuthenticationService = inject(AuthenticationService);
22-
23-
super(router, authenticationService);
24-
}
25-
2611
/**
2712
* Purpose of this guard is check that current user has been authenticated to
2813
* application. If user is not authenticated he/she is redirected to application

src/app/auth/guards/base-auth.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inject } from '@angular/core';
12
import { Router, UrlTree } from '@angular/router';
23
import { Observable } from 'rxjs';
34
import { map, take } from 'rxjs/operators';
@@ -6,20 +7,17 @@ import { AuthGuardMetaDataInterface } from 'src/app/auth/interfaces';
67
import { AuthenticationService } from 'src/app/auth/services';
78

89
export abstract class BaseAuth {
9-
/**
10-
* Constructor of the class. This is called from classes that extends this
11-
* abstract class.
12-
*/
13-
protected constructor(protected router: Router, protected authenticationService: AuthenticationService) { }
10+
private readonly router: Router = inject(Router);
11+
private readonly authenticationService: AuthenticationService = inject(AuthenticationService);
1412

1513
/**
1614
* Helper method to make check if user needs to be authenticated or not. This
1715
* is used from following guards:
1816
* - AnonymousGuard
1917
* - AuthenticationGuard
2018
*
21-
* By default this method will redirect user either to `/` or `/auth/login`
22-
* depending if user needs to be authenticated or not.
19+
* By default, this method will redirect user either to `/` or `/auth/login`
20+
* depending on if user needs to be authenticated or not.
2321
*
2422
* You can override this behaviour by setting `data` option to your route
2523
* definition where you can configure following;

src/app/auth/guards/base-role.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inject } from '@angular/core';
12
import { Router, UrlTree } from '@angular/router';
23
import { Store } from '@ngrx/store';
34
import { Observable } from 'rxjs';
@@ -7,11 +8,8 @@ import { RoleGuardMetaDataInterface } from 'src/app/auth/interfaces';
78
import { authenticationSelectors } from 'src/app/store';
89

910
export abstract class BaseRole {
10-
/**
11-
* Constructor of the class. This is called from classes that extends this
12-
* abstract class.
13-
*/
14-
protected constructor(protected router: Router, protected store: Store) { }
11+
private readonly router: Router = inject(Router);
12+
private readonly store: Store = inject(Store);
1513

1614
/**
1715
* Helper method to make check if user has certain role or not. This is used
@@ -21,8 +19,8 @@ export abstract class BaseRole {
2119
* - RoleRootGuard
2220
* - RoleUserGuard
2321
*
24-
* By default this method will redirect user either to `/` or `/auth/login`
25-
* depending if user is not logged in or user doesn't have the specified
22+
* By default, this method will redirect user either to `/` or `/auth/login`
23+
* depending on if user is not logged in or user doesn't have the specified
2624
* role.
2725
*
2826
* You can override this behaviour by setting `data` option to your route

src/app/auth/guards/role-admin.guard.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Injectable, inject } from '@angular/core';
2-
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
3-
import { Store } from '@ngrx/store';
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, UrlTree } from '@angular/router';
43
import { Observable } from 'rxjs';
54

65
import { Role } from 'src/app/auth/enums';
@@ -10,20 +9,6 @@ import { BaseRole } from 'src/app/auth/guards/base-role';
109
providedIn: 'root',
1110
})
1211
export class RoleAdminGuard extends BaseRole {
13-
protected readonly router: Router = inject(Router);
14-
protected readonly store: Store = inject(Store);
15-
16-
/**
17-
* Constructor of the class, where we DI all services that we need to use
18-
* within this guard.
19-
*/
20-
public constructor() {
21-
const router: Router = inject(Router);
22-
const store: Store = inject(Store);
23-
24-
super(router, store);
25-
}
26-
2712
/**
2813
* Purpose of this guard is to check that user has `Role.ROLE_ADMIN` or not.
2914
* This method is used within route definition `canActivate` definition.

src/app/auth/guards/role-logged.guard.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Injectable, inject } from '@angular/core';
2-
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
3-
import { Store } from '@ngrx/store';
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, UrlTree } from '@angular/router';
43
import { Observable } from 'rxjs';
54

65
import { Role } from 'src/app/auth/enums';
@@ -10,20 +9,6 @@ import { BaseRole } from 'src/app/auth/guards/base-role';
109
providedIn: 'root',
1110
})
1211
export class RoleALoggedGuard extends BaseRole {
13-
protected readonly router: Router = inject(Router);
14-
protected readonly store: Store = inject(Store);
15-
16-
/**
17-
* Constructor of the class, where we DI all services that we need to use
18-
* within this guard.
19-
*/
20-
public constructor() {
21-
const router: Router = inject(Router);
22-
const store: Store = inject(Store);
23-
24-
super(router, store);
25-
}
26-
2712
/**
2813
* Purpose of this guard is to check that user has `Role.ROLE_LOGGED` or not.
2914
* This method is used within route definition `canActivate` definition.

src/app/auth/guards/role-root.guard.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Injectable, inject } from '@angular/core';
2-
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
3-
import { Store } from '@ngrx/store';
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, UrlTree } from '@angular/router';
43
import { Observable } from 'rxjs';
54

65
import { Role } from 'src/app/auth/enums';
@@ -10,20 +9,6 @@ import { BaseRole } from 'src/app/auth/guards/base-role';
109
providedIn: 'root',
1110
})
1211
export class RoleRootGuard extends BaseRole {
13-
protected readonly router: Router = inject(Router);
14-
protected readonly store: Store = inject(Store);
15-
16-
/**
17-
* Constructor of the class, where we DI all services that we need to use
18-
* within this guard.
19-
*/
20-
public constructor() {
21-
const router: Router = inject(Router);
22-
const store: Store = inject(Store);
23-
24-
super(router, store);
25-
}
26-
2712
/**
2813
* Purpose of this guard is to check that user has `Role.ROLE_ROOT` or not.
2914
* This method is used within route definition `canActivate` definition.

src/app/auth/guards/role-user.guard.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Injectable, inject } from '@angular/core';
2-
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
3-
import { Store } from '@ngrx/store';
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, UrlTree } from '@angular/router';
43
import { Observable } from 'rxjs';
54

65
import { Role } from 'src/app/auth/enums';
@@ -10,20 +9,6 @@ import { BaseRole } from 'src/app/auth/guards/base-role';
109
providedIn: 'root',
1110
})
1211
export class RoleUserGuard extends BaseRole {
13-
protected readonly router: Router = inject(Router);
14-
protected readonly store: Store = inject(Store);
15-
16-
/**
17-
* Constructor of the class, where we DI all services that we need to use
18-
* within this guard.
19-
*/
20-
public constructor() {
21-
const router: Router = inject(Router);
22-
const store: Store = inject(Store);
23-
24-
super(router, store);
25-
}
26-
2712
/**
2813
* Purpose of this guard is to check that user has `Role.ROLE_USER` or not.
2914
* This method is used within route definition `canActivate` definition.

0 commit comments

Comments
 (0)