-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth)!: support asynchronous unauthenticated hooks (#2513)
BREAKING CHANGE: `DAFF_AUTH_UNAUTHENTICATED_HOOKS` contains functions that now must return an observable
- Loading branch information
Showing
17 changed files
with
283 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { daffAuthRoutingRedirectUnauthenticatedHookFactory } from './redirect-unauthenticated-hook-factory'; |
148 changes: 148 additions & 0 deletions
148
libs/auth/routing/src/helpers/redirect-unauthenticated-hook-factory.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { | ||
ActivatedRoute, | ||
ParamMap, | ||
Router, | ||
} from '@angular/router'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { DAFF_AUTH_ROUTING_CONFIG_DEFAULT } from '@daffodil/auth/routing'; | ||
import { | ||
DaffAuthActionTypes, | ||
DaffAuthLoginActionTypes, | ||
DaffAuthUnauthenticatedHook, | ||
} from '@daffodil/auth/state'; | ||
|
||
import { daffAuthRoutingRedirectUnauthenticatedHookFactory } from './redirect-unauthenticated-hook-factory'; | ||
|
||
|
||
describe('@daffodil/auth/routing | daffAuthRoutingRedirectUnauthenticatedHookFactory', () => { | ||
let router: Router; | ||
let route: ActivatedRoute; | ||
|
||
let hook: DaffAuthUnauthenticatedHook; | ||
let result: Observable<unknown>; | ||
|
||
let routerNavigateSpy: jasmine.Spy<Router['navigateByUrl']>; | ||
let qpSpy: jasmine.SpyObj<ParamMap>; | ||
let logoutRedirectUrl: string; | ||
let expirationRedirectUrl: string; | ||
let redirectUrl: string; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
RouterTestingModule, | ||
], | ||
}); | ||
|
||
router = TestBed.inject(Router); | ||
route = TestBed.inject(ActivatedRoute); | ||
|
||
logoutRedirectUrl = '/login'; | ||
expirationRedirectUrl = '/'; | ||
redirectUrl = '/redirect'; | ||
|
||
qpSpy = jasmine.createSpyObj('ParamMap', ['get']); | ||
routerNavigateSpy = spyOn(router, 'navigateByUrl'); | ||
routerNavigateSpy.and.returnValue(new Promise((resolve) => resolve(true))); | ||
|
||
hook = daffAuthRoutingRedirectUnauthenticatedHookFactory( | ||
router, | ||
<any>{ | ||
...route, | ||
snapshot: { | ||
...route.snapshot, | ||
queryParamMap: qpSpy, | ||
}, | ||
}, | ||
{ | ||
...DAFF_AUTH_ROUTING_CONFIG_DEFAULT, | ||
logoutRedirectPath: logoutRedirectUrl, | ||
tokenExpirationRedirectPath: expirationRedirectUrl, | ||
}, | ||
); | ||
}); | ||
|
||
describe('when the hook is triggered with DaffAuthLogoutSuccess', () => { | ||
beforeEach(() => { | ||
result = hook(DaffAuthLoginActionTypes.LogoutSuccessAction); | ||
}); | ||
|
||
it('should navigate to the login page', (done) => { | ||
result.subscribe(() => { | ||
expect(routerNavigateSpy).toHaveBeenCalledWith(logoutRedirectUrl); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('and when the redirect QP is set', () => { | ||
beforeEach(() => { | ||
qpSpy.get.withArgs('redirect').and.returnValue(redirectUrl); | ||
result = hook(DaffAuthLoginActionTypes.LogoutSuccessAction); | ||
}); | ||
|
||
it('should navigate to the redirect URL', (done) => { | ||
result.subscribe(() => { | ||
expect(routerNavigateSpy).toHaveBeenCalledWith(redirectUrl); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when the hook is triggered with DaffAuthCheckFailure', () => { | ||
beforeEach(() => { | ||
result = hook(DaffAuthActionTypes.AuthCheckFailureAction); | ||
}); | ||
|
||
it('should navigate to the home page', (done) => { | ||
result.subscribe(() => { | ||
expect(routerNavigateSpy).toHaveBeenCalledWith(expirationRedirectUrl); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('and when the redirect QP is set', () => { | ||
beforeEach(() => { | ||
qpSpy.get.withArgs('redirect').and.returnValue(redirectUrl); | ||
result = hook(DaffAuthActionTypes.AuthCheckFailureAction); | ||
}); | ||
|
||
it('should navigate to the redirect URL', (done) => { | ||
result.subscribe(() => { | ||
expect(routerNavigateSpy).toHaveBeenCalledWith(redirectUrl); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when the hook is triggered with DaffAuthGuardLogout', () => { | ||
beforeEach(() => { | ||
result = hook(DaffAuthActionTypes.AuthGuardLogoutAction); | ||
}); | ||
|
||
it('should navigate to the home page', (done) => { | ||
result.subscribe(() => { | ||
expect(routerNavigateSpy).toHaveBeenCalledWith(expirationRedirectUrl); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('and when the redirect QP is set', () => { | ||
beforeEach(() => { | ||
qpSpy.get.withArgs('redirect').and.returnValue(redirectUrl); | ||
result = hook(DaffAuthActionTypes.AuthGuardLogoutAction); | ||
}); | ||
|
||
it('should navigate to the redirect URL', (done) => { | ||
result.subscribe(() => { | ||
expect(routerNavigateSpy).toHaveBeenCalledWith(redirectUrl); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
libs/auth/routing/src/helpers/redirect-unauthenticated-hook-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
ActivatedRoute, | ||
Router, | ||
} from '@angular/router'; | ||
import { | ||
from, | ||
of, | ||
} from 'rxjs'; | ||
|
||
import { | ||
DaffAuthActionTypes, | ||
DaffAuthLoginActionTypes, | ||
DaffAuthUnauthenticatedHook, | ||
} from '@daffodil/auth/state'; | ||
|
||
import { DaffAuthRoutingConfig } from '../config/public_api'; | ||
|
||
export function daffAuthRoutingRedirectUnauthenticatedHookFactory(router: Router, route: ActivatedRoute, config: DaffAuthRoutingConfig): DaffAuthUnauthenticatedHook { | ||
return (trigger) => { | ||
switch (trigger) { | ||
case DaffAuthLoginActionTypes.LogoutSuccessAction: | ||
return from(router.navigateByUrl(route.snapshot.queryParamMap.get(config.redirectUrlParam) || config.logoutRedirectPath)); | ||
|
||
case DaffAuthActionTypes.AuthCheckFailureAction: | ||
case DaffAuthActionTypes.AuthGuardLogoutAction: | ||
return from(router.navigateByUrl(route.snapshot.queryParamMap.get(config.redirectUrlParam) || config.tokenExpirationRedirectPath)); | ||
|
||
default: | ||
return of(null); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './guards/public_api'; | ||
export * from './config/public_api'; | ||
export * from './helpers/public_api'; | ||
|
||
export * from './module'; | ||
export * from './redirect.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { | ||
NgModule, | ||
inject, | ||
} from '@angular/core'; | ||
import { | ||
ActivatedRoute, | ||
Router, | ||
} from '@angular/router'; | ||
import { EffectsModule } from '@ngrx/effects'; | ||
import { | ||
from, | ||
of, | ||
} from 'rxjs'; | ||
|
||
import { | ||
DAFF_AUTH_UNAUTHENTICATED_HOOKS, | ||
DaffAuthActionTypes, | ||
DaffAuthLoginActionTypes, | ||
DaffAuthUnauthenticatedHook, | ||
} from '@daffodil/auth/state'; | ||
|
||
import { DAFF_AUTH_ROUTING_CONFIG } from './config/public_api'; | ||
import { DaffAuthRedirectEffects } from './effects/redirect.effects'; | ||
import { daffAuthRoutingRedirectUnauthenticatedHookFactory } from './helpers/public_api'; | ||
|
||
|
||
@NgModule({ | ||
imports: [ | ||
EffectsModule.forFeature([ | ||
DaffAuthRedirectEffects, | ||
]), | ||
], | ||
providers: [ | ||
{ | ||
provide: DAFF_AUTH_UNAUTHENTICATED_HOOKS, | ||
multi: true, | ||
useFactory: () => | ||
daffAuthRoutingRedirectUnauthenticatedHookFactory( | ||
inject(Router), | ||
inject(ActivatedRoute), | ||
inject(DAFF_AUTH_ROUTING_CONFIG), | ||
), | ||
}, | ||
], | ||
}) | ||
export class DaffAuthRoutingRedirectModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.