Skip to content

Commit

Permalink
feat(auth): remove auth token in unauthenticated hook (#2523)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jul 31, 2023
1 parent f460cc8 commit 6472f8f
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 117 deletions.
10 changes: 0 additions & 10 deletions libs/auth/routing/src/guards/guest-only.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ describe('@daffodil/auth/routing | GuestOnlyGuard', () => {
expect(result).toBeObservable(expected);
});

it('should remove the token from storage', () => {
expect(result).toBeObservable(expected);
expect(daffAuthStorageService.removeAuthToken).toHaveBeenCalledWith();
});

it('should dispatch guard logout', () => {
expect(result).toBeObservable(expected);
expect(mockStore.dispatch).toHaveBeenCalledWith(jasmine.any(DaffAuthGuardLogout));
Expand All @@ -135,11 +130,6 @@ describe('@daffodil/auth/routing | GuestOnlyGuard', () => {
expect(result).toBeObservable(expected);
});

it('should remove the token from storage', () => {
expect(result).toBeObservable(expected);
expect(daffAuthStorageService.removeAuthToken).toHaveBeenCalledWith();
});

it('should dispatch guard logout', () => {
expect(result).toBeObservable(expected);
expect(mockStore.dispatch).toHaveBeenCalledWith(jasmine.any(DaffAuthGuardLogout));
Expand Down
1 change: 0 additions & 1 deletion libs/auth/routing/src/guards/guest-only.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export class GuestOnlyGuard implements CanActivate {
map(() => false),
catchError((error: DaffError) => {
if (DAFF_AUTH_UNAUTHENTICATED_ERROR_CODES[error.code]) {
this.storage.removeAuthToken();
this.store.dispatch(new DaffAuthGuardLogout(this.errorMatcher(error)));
}
return of(true);
Expand Down
10 changes: 0 additions & 10 deletions libs/auth/routing/src/guards/member-only.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ describe('@daffodil/auth/routing | MemberOnlyGuard', () => {
expect(result).toBeObservable(expected);
});

it('should remove the token from storage', () => {
expect(result).toBeObservable(expected);
expect(daffAuthStorageService.removeAuthToken).toHaveBeenCalledWith();
});

it('should dispatch guard logout', () => {
expect(result).toBeObservable(expected);
expect(mockStore.dispatch).toHaveBeenCalledWith(jasmine.any(DaffAuthGuardLogout));
Expand All @@ -135,11 +130,6 @@ describe('@daffodil/auth/routing | MemberOnlyGuard', () => {
expect(result).toBeObservable(expected);
});

it('should remove the token from storage', () => {
expect(result).toBeObservable(expected);
expect(daffAuthStorageService.removeAuthToken).toHaveBeenCalledWith();
});

it('should dispatch guard logout', () => {
expect(result).toBeObservable(expected);
expect(mockStore.dispatch).toHaveBeenCalledWith(jasmine.any(DaffAuthGuardLogout));
Expand Down
1 change: 0 additions & 1 deletion libs/auth/routing/src/guards/member-only.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export class MemberOnlyGuard implements CanActivate {
map(() => true),
catchError((error: DaffError) => {
if (DAFF_AUTH_UNAUTHENTICATED_ERROR_CODES[error.code]) {
this.storage.removeAuthToken();
this.store.dispatch(new DaffAuthGuardLogout(this.errorMatcher(error)));
}
return of(false);
Expand Down
27 changes: 27 additions & 0 deletions libs/auth/state/src/auth-state.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {
ModuleWithProviders,
NgModule,
inject,
} from '@angular/core';
import { EffectsModule } from '@ngrx/effects';
import { StoreModule } from '@ngrx/store';
import { of } from 'rxjs';

import { DaffAuthStorageService } from '@daffodil/auth';

import {
DaffAuthStateConfig,
Expand All @@ -14,9 +18,14 @@ import { DaffAuthEffects } from './effects/auth.effects';
import { DaffAuthLoginEffects } from './effects/login.effects';
import { DaffAuthRegisterEffects } from './effects/register.effects';
import { DaffAuthResetPasswordEffects } from './effects/reset-password.effects';
import {
DAFF_AUTH_UNAUTHENTICATED_HOOKS,
DaffAuthUnauthenticatedHook,
} from './injection-tokens/public_api';
import { DAFF_AUTH_STORE_FEATURE_KEY } from './reducers/public_api';
import { DAFF_AUTH_REDUCERS } from './reducers/token/reducers.token';


@NgModule({
imports: [
StoreModule.forFeature(DAFF_AUTH_STORE_FEATURE_KEY, DAFF_AUTH_REDUCERS),
Expand All @@ -27,6 +36,24 @@ import { DAFF_AUTH_REDUCERS } from './reducers/token/reducers.token';
DaffAuthResetPasswordEffects,
]),
],
providers: [
{
provide: DAFF_AUTH_UNAUTHENTICATED_HOOKS,
useFactory: () => {
const storage = inject(DaffAuthStorageService);
const hook: DaffAuthUnauthenticatedHook = () => {
try {
return of(storage.removeAuthToken());
} catch {
return of(null);
}
};

return hook;
},
multi: true,
},
],
})
export class DaffAuthStateModule {
static withConfig(config: Partial<DaffAuthStateConfig> = {}): ModuleWithProviders<DaffAuthStateModule> {
Expand Down
44 changes: 0 additions & 44 deletions libs/auth/state/src/effects/auth.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,50 +172,6 @@ describe('@daffodil/auth/state | DaffAuthEffects', () => {
});
});

describe('removeAuthToken$', () => {
let expected;

describe('when AuthCheckFailure is dispatched', () => {
let authLogoutSuccessAction: DaffAuthCheckFailure;

beforeEach(() => {
authLogoutSuccessAction = new DaffAuthCheckFailure({ code: 'code', message: 'message' });
actions$ = hot('--a', { a: authLogoutSuccessAction });
expected = cold('---');
});

it('should remove the auth token from storage', () => {
expect(effects.removeAuthToken$).toBeObservable(expected);
expect(removeTokenSpy).toHaveBeenCalledWith();
});

describe('and the storage service throws an error', () => {
beforeEach(() => {
removeTokenSpy.and.callFake(throwStorageError);

expected = cold('--(b|)', { b: authStorageFailureAction });
});

it('should return a DaffAuthStorageFailure', () => {
expect(effects.removeAuthToken$).toBeObservable(expected);
});
});

describe('and the storage service throws a server side error', () => {
beforeEach(() => {
const error = new DaffServerSideStorageError('Server side');
const serverSideAction = new DaffAuthServerSide(daffTransformErrorToStateError(error));
removeTokenSpy.and.throwError(error);
expected = cold('--(a|)', { a: serverSideAction });
});

it('should dispatch a server side action', () => {
expect(effects.removeAuthToken$).toBeObservable(expected);
});
});
});
});

describe('resetToUnauthenticated$', () => {
let expected;

Expand Down
20 changes: 0 additions & 20 deletions libs/auth/state/src/effects/auth.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,6 @@ export class DaffAuthEffects {
),
));

removeAuthToken$ = createEffect(() => this.actions$.pipe(
ofType(
DaffAuthActionTypes.AuthCheckFailureAction,
),
tap(() => {
this.storage.removeAuthToken();
}),
switchMap(() => EMPTY),
catchError((error: Error) => {
switch (true) {
case error instanceof DaffServerSideStorageError:
return of(new DaffAuthServerSide(this.errorMatcher(error)));

case error instanceof DaffStorageServiceError:
default:
return of(new DaffAuthStorageFailure(this.errorMatcher(error)));
}
}),
), { dispatch: false });

// this needs to be defined after `check$` or else the driver call won't be run
authCheckInterval$ = createEffect(() => of(new DaffAuthCheck()).pipe(
repeat({ delay: this.config.checkInterval }),
Expand Down
30 changes: 0 additions & 30 deletions libs/auth/state/src/effects/login.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,36 +187,6 @@ describe('@daffodil/auth/state | DaffAuthLoginEffects', () => {
it('should notify state that the logout succeeded', () => {
expect(effects.logout$).toBeObservable(expected);
});

it('should remove the auth token from storage', () => {
expect(effects.logout$).toBeObservable(expected);
expect(removeAuthTokenSpy).toHaveBeenCalledWith();
});

describe('unless the storage service throws an error', () => {
beforeEach(() => {
removeAuthTokenSpy.and.callFake(throwStorageError);

expected = cold('--(b)', { b: authStorageFailureAction });
});

it('should return a DaffAuthStorageFailure', () => {
expect(effects.logout$).toBeObservable(expected);
});
});

describe('unless the storage service throws a server side error', () => {
beforeEach(() => {
const error = new DaffServerSideStorageError('Server side');
const serverSideAction = new DaffAuthServerSide(daffTransformErrorToStateError(error));
removeAuthTokenSpy.and.throwError(error);
expected = cold('--(a)', { a: serverSideAction });
});

it('should dispatch a server side action', () => {
expect(effects.logout$).toBeObservable(expected);
});
});
});

describe('and the logout fails', () => {
Expand Down
1 change: 0 additions & 1 deletion libs/auth/state/src/effects/login.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export class DaffAuthLoginEffects<
switchMap((action: DaffAuthLogout) =>
this.loginDriver.logout().pipe(
map(() => new DaffAuthLogoutSuccess()),
tap(() => this.storage.removeAuthToken()),
catchError((error: DaffError) => {
switch (true) {
case error instanceof DaffServerSideStorageError:
Expand Down

0 comments on commit 6472f8f

Please sign in to comment.