Added persistent login

This commit is contained in:
Zakary Timson 2018-11-14 14:42:38 -05:00
parent 7f498ac980
commit 5d58979d6e

View File

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