From 01f14aecfdfa2b07f3d3ceb1f32bbe8c02f77886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20Lepp=C3=A4nen?= Date: Fri, 12 Sep 2025 18:02:25 +0300 Subject: [PATCH] Refactored guards to use simpler logic --- src/app/auth/guards/anonymous.guard.ts | 19 ++----------------- src/app/auth/guards/authentication.guard.ts | 19 ++----------------- src/app/auth/guards/base-auth.ts | 12 +++++------- src/app/auth/guards/base-role.ts | 12 +++++------- src/app/auth/guards/role-admin.guard.ts | 19 ++----------------- src/app/auth/guards/role-logged.guard.ts | 19 ++----------------- src/app/auth/guards/role-root.guard.ts | 19 ++----------------- src/app/auth/guards/role-user.guard.ts | 19 ++----------------- 8 files changed, 22 insertions(+), 116 deletions(-) diff --git a/src/app/auth/guards/anonymous.guard.ts b/src/app/auth/guards/anonymous.guard.ts index 876838f65..34046d1b0 100644 --- a/src/app/auth/guards/anonymous.guard.ts +++ b/src/app/auth/guards/anonymous.guard.ts @@ -1,28 +1,13 @@ -import { Injectable, inject } from '@angular/core'; -import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router'; +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; import { BaseAuth } from 'src/app/auth/guards/base-auth'; -import { AuthenticationService } from 'src/app/auth/services'; @Injectable({ providedIn: 'root', }) export class AnonymousGuard extends BaseAuth { - protected readonly router: Router = inject(Router); - protected readonly authenticationService: AuthenticationService = inject(AuthenticationService); - - /** - * Constructor of the class, where we DI all services that we need to use - * within this guard. - */ - public constructor() { - const router: Router = inject(Router); - const authenticationService: AuthenticationService = inject(AuthenticationService); - - super(router, authenticationService); - } - /** * Purpose of this guard is check that current user has not been authenticated * to application. If user is authenticated he/she is redirected to application diff --git a/src/app/auth/guards/authentication.guard.ts b/src/app/auth/guards/authentication.guard.ts index bc2853a4f..e59de7e8f 100644 --- a/src/app/auth/guards/authentication.guard.ts +++ b/src/app/auth/guards/authentication.guard.ts @@ -1,28 +1,13 @@ -import { Injectable, inject } from '@angular/core'; -import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router'; +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; import { BaseAuth } from 'src/app/auth/guards/base-auth'; -import { AuthenticationService } from 'src/app/auth/services'; @Injectable({ providedIn: 'root', }) export class AuthenticationGuard extends BaseAuth { - protected readonly router: Router = inject(Router); - protected readonly authenticationService: AuthenticationService = inject(AuthenticationService); - - /** - * Constructor of the class, where we DI all services that we need to use - * within this guard. - */ - public constructor() { - const router: Router = inject(Router); - const authenticationService: AuthenticationService = inject(AuthenticationService); - - super(router, authenticationService); - } - /** * Purpose of this guard is check that current user has been authenticated to * application. If user is not authenticated he/she is redirected to application diff --git a/src/app/auth/guards/base-auth.ts b/src/app/auth/guards/base-auth.ts index f99dd318e..ff6ad7251 100644 --- a/src/app/auth/guards/base-auth.ts +++ b/src/app/auth/guards/base-auth.ts @@ -1,3 +1,4 @@ +import { inject } from '@angular/core'; import { Router, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; import { map, take } from 'rxjs/operators'; @@ -6,11 +7,8 @@ import { AuthGuardMetaDataInterface } from 'src/app/auth/interfaces'; import { AuthenticationService } from 'src/app/auth/services'; export abstract class BaseAuth { - /** - * Constructor of the class. This is called from classes that extends this - * abstract class. - */ - protected constructor(protected router: Router, protected authenticationService: AuthenticationService) { } + private readonly router: Router = inject(Router); + private readonly authenticationService: AuthenticationService = inject(AuthenticationService); /** * Helper method to make check if user needs to be authenticated or not. This @@ -18,8 +16,8 @@ export abstract class BaseAuth { * - AnonymousGuard * - AuthenticationGuard * - * By default this method will redirect user either to `/` or `/auth/login` - * depending if user needs to be authenticated or not. + * By default, this method will redirect user either to `/` or `/auth/login` + * depending on if user needs to be authenticated or not. * * You can override this behaviour by setting `data` option to your route * definition where you can configure following; diff --git a/src/app/auth/guards/base-role.ts b/src/app/auth/guards/base-role.ts index d1d50412d..5f2c152a9 100644 --- a/src/app/auth/guards/base-role.ts +++ b/src/app/auth/guards/base-role.ts @@ -1,3 +1,4 @@ +import { inject } from '@angular/core'; import { Router, UrlTree } from '@angular/router'; import { Store } from '@ngrx/store'; import { Observable } from 'rxjs'; @@ -7,11 +8,8 @@ import { RoleGuardMetaDataInterface } from 'src/app/auth/interfaces'; import { authenticationSelectors } from 'src/app/store'; export abstract class BaseRole { - /** - * Constructor of the class. This is called from classes that extends this - * abstract class. - */ - protected constructor(protected router: Router, protected store: Store) { } + private readonly router: Router = inject(Router); + private readonly store: Store = inject(Store); /** * Helper method to make check if user has certain role or not. This is used @@ -21,8 +19,8 @@ export abstract class BaseRole { * - RoleRootGuard * - RoleUserGuard * - * By default this method will redirect user either to `/` or `/auth/login` - * depending if user is not logged in or user doesn't have the specified + * By default, this method will redirect user either to `/` or `/auth/login` + * depending on if user is not logged in or user doesn't have the specified * role. * * You can override this behaviour by setting `data` option to your route diff --git a/src/app/auth/guards/role-admin.guard.ts b/src/app/auth/guards/role-admin.guard.ts index fbbe92c81..a25102524 100644 --- a/src/app/auth/guards/role-admin.guard.ts +++ b/src/app/auth/guards/role-admin.guard.ts @@ -1,6 +1,5 @@ -import { Injectable, inject } from '@angular/core'; -import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router'; -import { Store } from '@ngrx/store'; +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; import { Role } from 'src/app/auth/enums'; @@ -10,20 +9,6 @@ import { BaseRole } from 'src/app/auth/guards/base-role'; providedIn: 'root', }) export class RoleAdminGuard extends BaseRole { - protected readonly router: Router = inject(Router); - protected readonly store: Store = inject(Store); - - /** - * Constructor of the class, where we DI all services that we need to use - * within this guard. - */ - public constructor() { - const router: Router = inject(Router); - const store: Store = inject(Store); - - super(router, store); - } - /** * Purpose of this guard is to check that user has `Role.ROLE_ADMIN` or not. * This method is used within route definition `canActivate` definition. diff --git a/src/app/auth/guards/role-logged.guard.ts b/src/app/auth/guards/role-logged.guard.ts index 9bac338b3..4748cba13 100644 --- a/src/app/auth/guards/role-logged.guard.ts +++ b/src/app/auth/guards/role-logged.guard.ts @@ -1,6 +1,5 @@ -import { Injectable, inject } from '@angular/core'; -import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router'; -import { Store } from '@ngrx/store'; +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; import { Role } from 'src/app/auth/enums'; @@ -10,20 +9,6 @@ import { BaseRole } from 'src/app/auth/guards/base-role'; providedIn: 'root', }) export class RoleALoggedGuard extends BaseRole { - protected readonly router: Router = inject(Router); - protected readonly store: Store = inject(Store); - - /** - * Constructor of the class, where we DI all services that we need to use - * within this guard. - */ - public constructor() { - const router: Router = inject(Router); - const store: Store = inject(Store); - - super(router, store); - } - /** * Purpose of this guard is to check that user has `Role.ROLE_LOGGED` or not. * This method is used within route definition `canActivate` definition. diff --git a/src/app/auth/guards/role-root.guard.ts b/src/app/auth/guards/role-root.guard.ts index 384634f50..6caec067e 100644 --- a/src/app/auth/guards/role-root.guard.ts +++ b/src/app/auth/guards/role-root.guard.ts @@ -1,6 +1,5 @@ -import { Injectable, inject } from '@angular/core'; -import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router'; -import { Store } from '@ngrx/store'; +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; import { Role } from 'src/app/auth/enums'; @@ -10,20 +9,6 @@ import { BaseRole } from 'src/app/auth/guards/base-role'; providedIn: 'root', }) export class RoleRootGuard extends BaseRole { - protected readonly router: Router = inject(Router); - protected readonly store: Store = inject(Store); - - /** - * Constructor of the class, where we DI all services that we need to use - * within this guard. - */ - public constructor() { - const router: Router = inject(Router); - const store: Store = inject(Store); - - super(router, store); - } - /** * Purpose of this guard is to check that user has `Role.ROLE_ROOT` or not. * This method is used within route definition `canActivate` definition. diff --git a/src/app/auth/guards/role-user.guard.ts b/src/app/auth/guards/role-user.guard.ts index d5e2c708f..14ff18cdc 100644 --- a/src/app/auth/guards/role-user.guard.ts +++ b/src/app/auth/guards/role-user.guard.ts @@ -1,6 +1,5 @@ -import { Injectable, inject } from '@angular/core'; -import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router'; -import { Store } from '@ngrx/store'; +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; import { Role } from 'src/app/auth/enums'; @@ -10,20 +9,6 @@ import { BaseRole } from 'src/app/auth/guards/base-role'; providedIn: 'root', }) export class RoleUserGuard extends BaseRole { - protected readonly router: Router = inject(Router); - protected readonly store: Store = inject(Store); - - /** - * Constructor of the class, where we DI all services that we need to use - * within this guard. - */ - public constructor() { - const router: Router = inject(Router); - const store: Store = inject(Store); - - super(router, store); - } - /** * Purpose of this guard is to check that user has `Role.ROLE_USER` or not. * This method is used within route definition `canActivate` definition.