diff --git a/src/app/login/login.guard.ts b/src/app/login/login.guard.ts index d22a7d9..e1265e7 100644 --- a/src/app/login/login.guard.ts +++ b/src/app/login/login.guard.ts @@ -1,17 +1,23 @@ import {CanActivate, Router} from '@angular/router'; import {firebaseApp} from '../app.module'; import {Injectable} from '@angular/core'; +import {Observable, timer} from 'rxjs'; +import {filter, map} from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class LoginGuard implements CanActivate { + loggedIn?; - constructor(private router: Router) {} + constructor(private router: Router) { + firebaseApp.auth().onAuthStateChanged(user => this.loggedIn = !!user); + } - canActivate() { - let valid = !!firebaseApp.auth().currentUser; - if(!valid) this.router.navigate(['/login']); - return valid; + canActivate(): Observable { + return timer(0, 100).pipe(filter(() => this.loggedIn != null), map(() => { + if(!this.loggedIn) this.router.navigate(['/login']); + return this.loggedIn; + })); } }