diff --git a/frontend/projects/upgrade/src/app/core/auth/auth.service.ts b/frontend/projects/upgrade/src/app/core/auth/auth.service.ts index faf7c14c8d..ead61f0071 100644 --- a/frontend/projects/upgrade/src/app/core/auth/auth.service.ts +++ b/frontend/projects/upgrade/src/app/core/auth/auth.service.ts @@ -9,12 +9,11 @@ import { selectGoogleCredential, } from './store/auth.selectors'; import { UserPermission } from './store/auth.models'; -import { BehaviorSubject } from 'rxjs'; +import { BehaviorSubject, filter, take } from 'rxjs'; import { AUTH_CONSTANTS, GoogleAuthJWTPayload, User, UserRole } from '../users/store/users.model'; import { ENV, Environment } from '../../../environments/environment-types'; import jwt_decode from 'jwt-decode'; -import { AuthDataService } from './auth.data.service'; -import { NavigationEnd, Router } from '@angular/router'; +import { NavigationEnd, NavigationSkipped, Router } from '@angular/router'; @Injectable() export class AuthService { @@ -26,7 +25,6 @@ export class AuthService { constructor( private store$: Store, - private authDataService: AuthDataService, private router: Router, private ngZone: NgZone, private localStorageService: LocalStorageService, @@ -129,15 +127,17 @@ export class AuthService { } // wait after google auth login navs back to app on success to dispatch data fetches + // we want to simply wait until we receive NavigationEnd or NavigationSkipped (when the redirectUrl is the same as the current url) + // then we once want to fire this one time to fetch the authed user data in db per permissions and complete the sub, so we use take(1) deferFetchUserExperimentDataAfterNavigationEnd(user: User, googleCredential: string): void { - let hasFired = false; - - this.router.events.pipe().subscribe((event) => { - if (!hasFired && event instanceof NavigationEnd) { - hasFired = true; + this.router.events + .pipe( + filter((event) => event instanceof NavigationEnd || event instanceof NavigationSkipped), + take(1) + ) + .subscribe(() => { this.store$.dispatch(AuthActions.actionFetchUserExperimentData({ user: { ...user, token: googleCredential } })); - } - }); + }); } setUserSettingsWithRole(user: User, actions: Action[]): Action[] { diff --git a/frontend/projects/upgrade/src/app/core/auth/store/auth.effects.ts b/frontend/projects/upgrade/src/app/core/auth/store/auth.effects.ts index c2571f6213..6a696c3f0c 100755 --- a/frontend/projects/upgrade/src/app/core/auth/store/auth.effects.ts +++ b/frontend/projects/upgrade/src/app/core/auth/store/auth.effects.ts @@ -1,4 +1,4 @@ -import { Injectable, Inject } from '@angular/core'; +import { Injectable } from '@angular/core'; import { Actions, createEffect, ofType } from '@ngrx/effects'; import * as authActions from './auth.actions'; import * as experimentUserActions from '../../experiment-users/store/experiment-users.actions'; @@ -14,8 +14,6 @@ import { selectRedirectUrl } from './auth.selectors'; import { AuthDataService } from '../auth.data.service'; import { AuthService } from '../auth.service'; import { User } from '../../users/store/users.model'; -import { SettingsService } from '../../settings/settings.service'; -import { ENV, Environment } from '../../../../environments/environment-types'; @Injectable() export class AuthEffects { @@ -24,9 +22,7 @@ export class AuthEffects { private store$: Store, private router: Router, private authDataService: AuthDataService, - private authService: AuthService, - private settingsService: SettingsService, - @Inject(ENV) private environment: Environment + private authService: AuthService ) {} fetchUserExperimentData$ = createEffect(() =>