Skip to content

Commit

Permalink
fix(auth): auth check interval prevents app from becoming stable (#3200)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent 8ccd628 commit b4d0619
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions libs/auth/state/src/effects/auth.effects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Injectable,
Inject,
NgZone,
} from '@angular/core';
import {
Actions,
Expand All @@ -10,12 +11,12 @@ import {
import {
of,
EMPTY,
Observable,
} from 'rxjs';
import {
switchMap,
catchError,
map,
repeat,
filter,
tap,
} from 'rxjs/operators';
Expand Down Expand Up @@ -68,6 +69,7 @@ export class DaffAuthEffects {
@Inject(DAFF_AUTH_STATE_CONFIG) private config: DaffAuthStateConfig,
@Inject(DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE) private clientCache: DaffDriverHttpClientCacheServiceInterface,
@Inject(DAFF_AUTH_UNAUTHENTICATED_HOOK) private unauthenticatedHook: DaffAuthUnauthenticatedHook,
private zone: NgZone,
) {}

check$ = createEffect(() => this.actions$.pipe(
Expand All @@ -81,22 +83,35 @@ export class DaffAuthEffects {
));

// 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 }),
filter(() => !!this.storage.getAuthToken()),
catchError((error: Error) => {
switch (true) {
case error instanceof DaffServerSideStorageError:
return of(new DaffAuthServerSide(this.errorMatcher(error)));
authCheckInterval$ = createEffect(() =>
new Observable<DaffAuthCheck>((subscriber) => {
const intervalId = this.zone.runOutsideAngular(() =>
setInterval(() => {
subscriber.next(new DaffAuthCheck());
}, this.config.checkInterval),
);

case error instanceof DaffStorageServiceError:
return of(new DaffAuthStorageFailure(this.errorMatcher(error)));
subscriber.next(new DaffAuthCheck());

default:
return EMPTY;
}
}),
));
return () => {
clearInterval(intervalId);
};
}).pipe(
filter(() => !!this.storage.getAuthToken()),
catchError((error: Error) => {
switch (true) {
case error instanceof DaffServerSideStorageError:
return of(new DaffAuthServerSide(this.errorMatcher(error)));

case error instanceof DaffStorageServiceError:
return of(new DaffAuthStorageFailure(this.errorMatcher(error)));

default:
return EMPTY;
}
}),
),
);

resetToUnauthenticated$ = createEffect(() => this.actions$.pipe(
ofType(
Expand Down

0 comments on commit b4d0619

Please sign in to comment.