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
19 changes: 2 additions & 17 deletions src/app/auth/guards/anonymous.guard.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 2 additions & 17 deletions src/app/auth/guards/authentication.guard.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 5 additions & 7 deletions src/app/auth/guards/base-auth.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -6,20 +7,17 @@ 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
* is used from following guards:
* - 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;
Expand Down
12 changes: 5 additions & 7 deletions src/app/auth/guards/base-role.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { inject } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 2 additions & 17 deletions src/app/auth/guards/role-admin.guard.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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.
Expand Down
19 changes: 2 additions & 17 deletions src/app/auth/guards/role-logged.guard.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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.
Expand Down
19 changes: 2 additions & 17 deletions src/app/auth/guards/role-root.guard.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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.
Expand Down
19 changes: 2 additions & 17 deletions src/app/auth/guards/role-user.guard.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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.
Expand Down
Loading