Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/1252 auth refresh fix #1275

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading