Skip to content

Commit

Permalink
Refresh user profile on ID token change (#2132)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwkotto authored Jan 21, 2025
1 parent b4bee27 commit 7f2cdfe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion web/src/app/services/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ describe('AuthService', () => {
providers: [
{provide: AngularFirestore, useValue: {}},
{provide: AngularFireFunctions, useValue: {}},
{provide: AngularFireAuth, useValue: {authState: NEVER}},
{
provide: AngularFireAuth,
useValue: {
authState: NEVER,
onIdTokenChanged: (callback: Function) => callback(null),
},
},
{provide: DataStoreService, useValue: {user$: () => of()}},
{provide: Router, useValue: {}},
{provide: HttpClientService, useValue: {}},
Expand Down
7 changes: 5 additions & 2 deletions web/src/app/services/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {AngularFireAuth} from '@angular/fire/compat/auth';
import {AngularFireFunctions} from '@angular/fire/compat/functions';
import {GoogleAuthProvider} from 'firebase/auth';
import firebase from 'firebase/compat/app';
import {Observable, firstValueFrom, from} from 'rxjs';
import {map, shareReplay, switchMap} from 'rxjs/operators';
import {Observable, Subject, firstValueFrom, from} from 'rxjs';
import {map, mergeWith, shareReplay, switchMap} from 'rxjs/operators';

import {AclEntry} from 'app/models/acl-entry.model';
import {DataCollectionStrategy, Job} from 'app/models/job.model';
Expand Down Expand Up @@ -52,6 +52,7 @@ export const ROLE_OPTIONS = [
})
export class AuthService {
private user$: Observable<User>;
private tokenChanged$ = new Subject<firebase.User | null>();
private currentUser!: User;
private hasAcceptedTos = false;

Expand All @@ -62,7 +63,9 @@ export class AuthService {
private functions: AngularFireFunctions,
private httpClientService: HttpClientService
) {
this.afAuth.onIdTokenChanged(user => this.tokenChanged$.next(user));
this.user$ = this.afAuth.authState.pipe(
mergeWith(this.tokenChanged$),
switchMap(user => from(this.onAuthStateChange(user))),
map(user => user || ANONYMOUS_USER),
// Cache last authenticated user so that late subscribers receive it as well.
Expand Down

0 comments on commit 7f2cdfe

Please sign in to comment.