Skip to content

Commit

Permalink
feat(auth): add unauthenticated hook tokens (#2503)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jul 13, 2023
1 parent 11b1bb1 commit 0f0d0c3
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/auth/state/src/injection-tokens/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './unauthenticated/public_api';
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 libs/auth/state/src/injection-tokens/unauthenticated/hook.token.ts
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();
}, () => {}),
},
);
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);
});
});
});
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,
}));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
daffAuthProvideUnauthenticatedHooks,
DAFF_AUTH_UNAUTHENTICATED_HOOKS,
} from './hooks.token';
1 change: 1 addition & 0 deletions libs/auth/state/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './reducers/public_api';
export * from './selectors/public_api';
export * from './facades/public_api';
export * from './config/public_api';
export * from './injection-tokens/public_api';

export { DaffAuthStateModule } from './auth-state.module';

0 comments on commit 0f0d0c3

Please sign in to comment.