fix login redirect
This commit is contained in:
@@ -85,6 +85,7 @@ describe('UserSessionService', () => {
|
||||
const updateSpy = jasmine.createSpy('update').and.resolveTo();
|
||||
dbServiceSpy.doc.and.returnValue({update: updateSpy} as never);
|
||||
runInFirebaseContextSpy.and.resolveTo({user: {uid: 'user-1'}});
|
||||
authStateSubject.next({uid: 'user-1'});
|
||||
|
||||
await expectAsync(service.login('mail', 'secret')).toBeResolvedTo('user-1');
|
||||
|
||||
@@ -92,6 +93,23 @@ describe('UserSessionService', () => {
|
||||
expect(updateSpy).toHaveBeenCalledWith({songUsage: {}});
|
||||
});
|
||||
|
||||
it('should wait for auth state propagation before resolving login', async () => {
|
||||
runInFirebaseContextSpy.and.resolveTo({user: {uid: 'user-1'}});
|
||||
|
||||
let resolved = false;
|
||||
const loginPromise = service.login('mail', 'secret').then(result => {
|
||||
resolved = true;
|
||||
return result;
|
||||
});
|
||||
|
||||
await Promise.resolve();
|
||||
expect(resolved).toBeFalse();
|
||||
|
||||
authStateSubject.next({uid: 'user-1'});
|
||||
|
||||
await expectAsync(loginPromise).toBeResolvedTo('user-1');
|
||||
});
|
||||
|
||||
it('should delegate logout and password reset to AngularFire auth APIs', async () => {
|
||||
runInFirebaseContextSpy.and.resolveTo();
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import {EnvironmentInjector, Injectable, inject, runInInjectionContext} from '@a
|
||||
import {Auth, authState, createUserWithEmailAndPassword, sendPasswordResetEmail, signInWithEmailAndPassword, signOut} from '@angular/fire/auth';
|
||||
import {User as AuthUser} from 'firebase/auth';
|
||||
import {firstValueFrom, Observable, of} from 'rxjs';
|
||||
import {map, shareReplay, switchMap} from 'rxjs/operators';
|
||||
import {filter, map, shareReplay, switchMap, take} from 'rxjs/operators';
|
||||
import {DbService} from '../db.service';
|
||||
import {environment} from '../../../environments/environment';
|
||||
import {Router} from '@angular/router';
|
||||
@@ -59,6 +59,7 @@ export class UserSessionService {
|
||||
const dUser = await this.readUser(aUser.user.uid);
|
||||
if (!dUser) return null;
|
||||
await this.initSongUsage(dUser);
|
||||
await this.awaitAuthenticatedUser(aUser.user.uid);
|
||||
|
||||
return aUser.user.uid;
|
||||
}
|
||||
@@ -90,6 +91,15 @@ export class UserSessionService {
|
||||
await this.update$(user.id, {songUsage: {}});
|
||||
}
|
||||
|
||||
private async awaitAuthenticatedUser(uid: string): Promise<void> {
|
||||
await firstValueFrom(
|
||||
this.user$.pipe(
|
||||
filter((user): user is User => !!user && user.id === uid),
|
||||
take(1)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private runInFirebaseContext = <T>(factory: () => T): T => runInInjectionContext(this.environmentInjector, factory);
|
||||
private readUser$ = (uid: string) => this.db.doc$<User>('users/' + uid);
|
||||
private readUser = (uid: string): Promise<User | null> => firstValueFrom(this.readUser$(uid));
|
||||
|
||||
Reference in New Issue
Block a user