Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use RedirectCommand in auth guard #413

Merged
merged 1 commit into from
Oct 2, 2024
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
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