-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add unauthenticated hook tokens (#2503)
- Loading branch information
Showing
7 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './unauthenticated/public_api'; |
33 changes: 33 additions & 0 deletions
33
libs/auth/state/src/injection-tokens/unauthenticated/hook.token.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,33 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { daffAuthProvideUnauthenticatedHooks } from '@daffodil/auth/state'; | ||
|
||
import { DAFF_AUTH_UNAUTHENTICATED_HOOK } from './hook.token'; | ||
|
||
describe('@daffodil/auth/state | DAFF_AUTH_UNAUTHENTICATED_HOOK', () => { | ||
let spy1: jasmine.Spy; | ||
let spy2: jasmine.Spy; | ||
let result: () => void; | ||
|
||
beforeEach(() => { | ||
spy1 = jasmine.createSpy(); | ||
spy2 = jasmine.createSpy(); | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [ | ||
...daffAuthProvideUnauthenticatedHooks( | ||
spy1, | ||
spy2, | ||
), | ||
], | ||
}); | ||
|
||
result = TestBed.inject(DAFF_AUTH_UNAUTHENTICATED_HOOK); | ||
}); | ||
|
||
it('should provide a function that calls all the hooks', () => { | ||
result(); | ||
expect(spy1).toHaveBeenCalledWith(); | ||
expect(spy2).toHaveBeenCalledWith(); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
libs/auth/state/src/injection-tokens/unauthenticated/hook.token.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,22 @@ | ||
import { | ||
inject, | ||
InjectionToken, | ||
} from '@angular/core'; | ||
|
||
import { DAFF_AUTH_UNAUTHENTICATED_HOOKS } from './hooks.token'; | ||
|
||
/** | ||
* An internal token to hold the unauthenticated hook. | ||
* Combines the multi provided hooks into a single function. | ||
* | ||
* @docs-private | ||
*/ | ||
export const DAFF_AUTH_UNAUTHENTICATED_HOOK = new InjectionToken<() => void>( | ||
'DAFF_AUTH_UNAUTHENTICATED_HOOK', | ||
{ | ||
factory: () => inject(DAFF_AUTH_UNAUTHENTICATED_HOOKS).reduce((acc, hook) => () => { | ||
acc(); | ||
hook(); | ||
}, () => {}), | ||
}, | ||
); |
32 changes: 32 additions & 0 deletions
32
libs/auth/state/src/injection-tokens/unauthenticated/hooks.token.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,32 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { | ||
daffAuthProvideUnauthenticatedHooks, | ||
DAFF_AUTH_UNAUTHENTICATED_HOOKS, | ||
} from './hooks.token'; | ||
|
||
describe('@daffodil/auth/state | daffAuthProvideUnauthenticatedHooks', () => { | ||
let hooks: (() => void)[]; | ||
let result: (() => void)[]; | ||
|
||
beforeEach(() => { | ||
hooks = [ | ||
() => {}, | ||
() => {}, | ||
]; | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [ | ||
...daffAuthProvideUnauthenticatedHooks(...hooks), | ||
], | ||
}); | ||
|
||
result = TestBed.inject(DAFF_AUTH_UNAUTHENTICATED_HOOKS); | ||
}); | ||
|
||
it('should provide the hooks to the token', () => { | ||
hooks.forEach(hook => { | ||
expect(result).toContain(hook); | ||
}); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
libs/auth/state/src/injection-tokens/unauthenticated/hooks.token.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,38 @@ | ||
import { | ||
InjectionToken, | ||
Provider, | ||
} from '@angular/core'; | ||
|
||
/** | ||
* A token to hold the unauthenticated hooks. | ||
* When a logged-in user is unauthenticated, various packages may perform cleanup tasks. | ||
* These are guaranteed to run synchronously before the client driver state is fully reset. | ||
* | ||
* Prefer using {@link daffAuthProvideUnauthenticatedHooks}. | ||
*/ | ||
export const DAFF_AUTH_UNAUTHENTICATED_HOOKS = new InjectionToken<(() => void)[]>( | ||
'DAFF_AUTH_UNAUTHENTICATED_HOOKS', | ||
{ factory: () => []}, | ||
); | ||
|
||
/** | ||
* Provides {@link DAFF_AUTH_UNAUTHENTICATED_HOOKS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffAuthProvideUnauthenticatedHooks( | ||
* myReducer1, | ||
* myReducer2 | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffAuthProvideUnauthenticatedHooks( | ||
...hooks: (() => void)[] | ||
): Provider[] { | ||
return hooks.map(hook => ({ | ||
provide: DAFF_AUTH_UNAUTHENTICATED_HOOKS, | ||
useValue: hook, | ||
multi: true, | ||
})); | ||
} |
4 changes: 4 additions & 0 deletions
4
libs/auth/state/src/injection-tokens/unauthenticated/public_api.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,4 @@ | ||
export { | ||
daffAuthProvideUnauthenticatedHooks, | ||
DAFF_AUTH_UNAUTHENTICATED_HOOKS, | ||
} from './hooks.token'; |
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