From 3f072f19ec46085d6610c0d95153c9a8bffb394a Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 6 Apr 2020 17:44:15 +0200 Subject: [PATCH] Fix: auth guard reactive user detection --- src/app/user/auth.guard.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/app/user/auth.guard.ts b/src/app/user/auth.guard.ts index 12b9bbae..c93d2849 100644 --- a/src/app/user/auth.guard.ts +++ b/src/app/user/auth.guard.ts @@ -6,6 +6,8 @@ import { } from '@angular/router'; import { AngularFireAuth } from '@angular/fire/auth'; import { SnackService } from '../services/snack.service'; +import { Observable } from 'rxjs'; +import { map, tap } from 'rxjs/operators'; @Injectable({ providedIn: 'root' @@ -15,16 +17,18 @@ export class AuthGuard implements CanActivate { private afAuth: AngularFireAuth, private snack: SnackService, ) {} - async canActivate( + canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot - ): Promise { - const user = await this.afAuth.auth.currentUser; - const isLoggedIn = !!user; - if (!isLoggedIn) { - this.snack.authError(); - } - return isLoggedIn; - } +): Observable { + return this.afAuth.user.pipe( + map((user): boolean => !!user), + tap((loggedIn: boolean) => { + if (!loggedIn) { + this.snack.authError(); + } + }) + ); +} }