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 {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<boolean> {
return timer(0, 100).pipe(filter(() => this.loggedIn != null), map(() => {
if(!this.loggedIn) this.router.navigate(['/login']);
return this.loggedIn;
}));
}
}