Skip to content

Commit

Permalink
Bugfix/1252 auth refresh fix (#1275)
Browse files Browse the repository at this point in the history
* include NavigationSkipped in event filter for action types that will dispatch fetchUserDetails, small refactor to do in proper rxjs fashion

* unused imports
  • Loading branch information
danoswaltCL authored Jan 31, 2024
1 parent 858d11e commit c0d92be
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
22 changes: 11 additions & 11 deletions frontend/projects/upgrade/src/app/core/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,7 +25,6 @@ export class AuthService {

constructor(
private store$: Store<AppState>,
private authDataService: AuthDataService,
private router: Router,
private ngZone: NgZone,
private localStorageService: LocalStorageService,
Expand Down Expand Up @@ -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[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand All @@ -24,9 +22,7 @@ export class AuthEffects {
private store$: Store<AppState>,
private router: Router,
private authDataService: AuthDataService,
private authService: AuthService,
private settingsService: SettingsService,
@Inject(ENV) private environment: Environment
private authService: AuthService
) {}

fetchUserExperimentData$ = createEffect(() =>
Expand Down

0 comments on commit c0d92be

Please sign in to comment.