diff --git a/projects/gameboard-ui/src/app/components/sponsor-select-banner/sponsor-select-banner.component.ts b/projects/gameboard-ui/src/app/components/sponsor-select-banner/sponsor-select-banner.component.ts index 179505c8..f880c1ad 100644 --- a/projects/gameboard-ui/src/app/components/sponsor-select-banner/sponsor-select-banner.component.ts +++ b/projects/gameboard-ui/src/app/components/sponsor-select-banner/sponsor-select-banner.component.ts @@ -1,5 +1,5 @@ import { Component } from '@angular/core'; -import { Observable, map, tap } from 'rxjs'; +import { Observable, filter, map, tap } from 'rxjs'; import { LogService } from '@/services/log.service'; import { RouterService } from '@/services/router.service'; import { ConfigService } from '@/utility/config.service'; @@ -42,7 +42,8 @@ export class SponsorSelectBannerComponent { this.sponsor$ = localUserService .user$ .pipe( - map(u => u?.sponsor) + map(u => u?.sponsor), + filter(s => !!s) ); } } diff --git a/projects/gameboard-ui/src/app/utility/auth.service.ts b/projects/gameboard-ui/src/app/utility/auth.service.ts index aeca4b47..697ea3e4 100644 --- a/projects/gameboard-ui/src/app/utility/auth.service.ts +++ b/projects/gameboard-ui/src/app/utility/auth.service.ts @@ -130,14 +130,7 @@ export class AuthService { } async externalLoginCallback(url?: string): Promise { - const user = await this.mgr.signinRedirectCallback(url); - - if (this.access_token()) { - // log the login event for the current user (we track date of last login and total login count) - await firstValueFrom(this.userService.updateLoginEvents()); - } - - return user; + return await this.mgr.signinRedirectCallback(url); } logout(): void { diff --git a/projects/gameboard-ui/src/app/utility/user.service.ts b/projects/gameboard-ui/src/app/utility/user.service.ts index c62fe495..9fa3ced6 100644 --- a/projects/gameboard-ui/src/app/utility/user.service.ts +++ b/projects/gameboard-ui/src/app/utility/user.service.ts @@ -3,13 +3,14 @@ import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; -import { BehaviorSubject, combineLatest, of, timer } from 'rxjs'; -import { catchError, filter, map, switchMap, tap } from 'rxjs/operators'; +import { BehaviorSubject, combineLatest, firstValueFrom, of, timer } from 'rxjs'; +import { catchError, distinctUntilChanged, filter, map, switchMap, tap } from 'rxjs/operators'; import { ApiUser, UserOidcProfile } from '../api/user-models'; import { UserService as ApiUserService } from '../api/user.service'; import { AuthService, AuthTokenState } from './auth.service'; import { ConfigService } from './config.service'; import { LogService } from '@/services/log.service'; +import { UnsubscriberService } from '@/services/unsubscriber.service'; @Injectable({ providedIn: 'root' }) export class UserService { @@ -20,6 +21,7 @@ export class UserService { constructor( private auth: AuthService, private log: LogService, + private unsub: UnsubscriberService, api: ApiUserService, config: ConfigService, router: Router @@ -56,6 +58,17 @@ export class UserService { this.user$.next(null); router.navigate(['/login']); }); + + // log the login event for the current user (we track date of last login and total login count) + this.unsub.add( + this.user$.pipe( + // when the user's id changes + distinctUntilChanged((prev, current) => (prev?.id || null) === (current?.id || null)), + // and when the user is truthy + filter(u => !!u), + // record a login event + ).subscribe(async u => await firstValueFrom(api.updateLoginEvents())) + ); } logout(): void {