Skip to content

Commit

Permalink
refactor: use RedirectCommand in auth guard
Browse files Browse the repository at this point in the history
  • Loading branch information
jrassa committed Oct 2, 2024
1 parent 0036485 commit cb728dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
22 changes: 11 additions & 11 deletions src/app/core/auth/auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { signal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { ActivatedRouteSnapshot, RedirectCommand, RouterStateSnapshot } from '@angular/router';

import { of } from 'rxjs';

Expand Down Expand Up @@ -60,8 +60,8 @@ describe('AuthGuard', () => {

TestBed.runInInjectionContext(() => {
authGuard()(route, {} as RouterStateSnapshot).subscribe((result) => {
expect(result instanceof UrlTree).toBe(true);
expect(result.toString()).toBe('/signin');
expect(result instanceof RedirectCommand).toBe(true);
expect((result as RedirectCommand).redirectTo.toString()).toBe('/signin');
done();
});
});
Expand All @@ -79,8 +79,8 @@ describe('AuthGuard', () => {

TestBed.runInInjectionContext(() => {
authGuard()(route, {} as RouterStateSnapshot).subscribe((result) => {
expect(result instanceof UrlTree).toBe(true);
expect(result.toString()).toBe('/unauthorized');
expect(result instanceof RedirectCommand).toBe(true);
expect((result as RedirectCommand).redirectTo.toString()).toBe('/unauthorized');
done();
});
});
Expand Down Expand Up @@ -120,8 +120,8 @@ describe('AuthGuard', () => {

TestBed.runInInjectionContext(() => {
authGuard()(route, {} as RouterStateSnapshot).subscribe((result) => {
expect(result instanceof UrlTree).toBe(true);
expect(result.toString()).toBe('/unauthorized');
expect(result instanceof RedirectCommand).toBe(true);
expect((result as RedirectCommand).redirectTo.toString()).toBe('/unauthorized');
done();
});
});
Expand All @@ -142,8 +142,8 @@ describe('AuthGuard', () => {

TestBed.runInInjectionContext(() => {
authGuard()(route, {} as RouterStateSnapshot).subscribe((result) => {
expect(result instanceof UrlTree).toBe(true);
expect(result.toString()).toBe('/unauthorized');
expect(result instanceof RedirectCommand).toBe(true);
expect((result as RedirectCommand).redirectTo.toString()).toBe('/unauthorized');
done();
});
});
Expand Down Expand Up @@ -207,8 +207,8 @@ describe('AuthGuard', () => {

TestBed.runInInjectionContext(() => {
authGuard()(route, {} as RouterStateSnapshot).subscribe((result) => {
expect(result instanceof UrlTree).toBe(true);
expect(result.toString()).toBe('/eua');
expect(result instanceof RedirectCommand).toBe(true);
expect((result as RedirectCommand).redirectTo.toString()).toBe('/eua');
done();
});
});
Expand Down
18 changes: 12 additions & 6 deletions src/app/core/auth/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import {
ActivatedRouteSnapshot,
GuardResult,
RedirectCommand,
Router,
RouterStateSnapshot
} from '@angular/router';

import { of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
Expand Down Expand Up @@ -58,18 +64,18 @@ export function authGuard(configOrRoles?: string | string[] | Partial<AuthGuardC

return session$.pipe(
switchMap(() => sessionService.getCurrentEua()),
map((): boolean | UrlTree => {
map((): GuardResult => {
// The user still isn't authenticated
if (!session().isAuthenticated()) {
return router.parseUrl('/signin');
return new RedirectCommand(router.parseUrl('/signin'));
}

// Check to see if the user needs to agree to the end user agreement
if (config.requiresEua && !session().isEuaCurrent()) {
return router.parseUrl('/eua');
return new RedirectCommand(router.parseUrl('/eua'));
}

if (!session().isAdmin()) {
if (!session().isAdmin() || true) {
// -----------------------------------------------------------
// Check the role requirements for the route
// -----------------------------------------------------------
Expand All @@ -78,7 +84,7 @@ export function authGuard(configOrRoles?: string | string[] | Partial<AuthGuardC
!session().hasSomeRoles(config.roles)
) {
// The user doesn't have the needed roles to view the page
return router.parseUrl('/unauthorized');
return new RedirectCommand(router.parseUrl('/unauthorized'));
}
}

Expand Down

0 comments on commit cb728dc

Please sign in to comment.