Updated auth module

This commit is contained in:
Zakary Timson 2019-08-03 12:39:22 -04:00
parent daeb960d9e
commit c52d38f49b
5 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,37 @@
import {Injectable} from "@angular/core";
import {BehaviorSubject, from} from "rxjs";
import {AngularFirestore} from "@angular/fire/firestore";
import {AngularFireAuth} from "@angular/fire/auth";
import {auth} from 'firebase';
import {Router} from "@angular/router";
import {flatMap, map, skip} from 'rxjs/operators';
import {User} from './user';
@Injectable({
providedIn: 'root'
})
export class AuthService {
readonly collection = 'Users';
user = new BehaviorSubject<User>(null);
constructor(private afAuth: AngularFireAuth, private router: Router, private db: AngularFirestore) {
this.afAuth.user.pipe(
flatMap((user: any) => {
if(!user) return from([false]);
let ref = this.db.collection(this.collection).doc(user.uid);
return ref.valueChanges().pipe(map(dbUser => Object.assign({ref: ref}, user, dbUser)))
})
).subscribe(user => this.user.next(<User>user));
}
async loginWithGoogle() {
this.afAuth.auth.signInWithPopup(new auth.GoogleAuthProvider());
return this.user.pipe(skip(1));
}
async logout() {
await this.afAuth.auth.signOut();
return this.router.navigate(['/']);
}
}

View File

@ -0,0 +1,17 @@
import {CanActivate, Router} from '@angular/router';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {filter, map, tap} from 'rxjs/operators';
import {AuthService} from '../auth.service';
@Injectable({
providedIn: 'root'
})
export class AdminGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) {}
canActivate(): Observable<boolean> {
return this.auth.user.pipe(filter((user: any) => user != null), map(user => user && user.isAdmin));
}
}

View File

@ -0,0 +1,17 @@
import {CanActivate, Router} from '@angular/router';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {filter, map, tap} from 'rxjs/operators';
import {AuthService} from '../auth.service';
@Injectable({
providedIn: 'root'
})
export class GuestGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) {}
canActivate(): Observable<boolean> {
return this.auth.user.pipe(filter((user: any) => user != null), map(user => user && user.isAnonymous), tap(auth => auth ? this.router.navigate(['/']) : null));
}
}

View File

@ -0,0 +1,17 @@
import {CanActivate, Router} from '@angular/router';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {filter, map, tap} from 'rxjs/operators';
import {AuthService} from '../auth.service';
@Injectable({
providedIn: 'root'
})
export class LoginGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) {}
canActivate(): Observable<boolean> {
return this.auth.user.pipe(filter((user: any) => user != null), map(user => user && !user.isAnonymous), tap(auth => auth ? null : this.router.navigate(['/login'])));
}
}

9
src/app/auth/user.ts Normal file
View File

@ -0,0 +1,9 @@
import {User as FirebaseUser} from "firebase"
import {AngularFirestoreDocument} from '@angular/fire/firestore';
export interface User extends FirebaseUser {
ref?: AngularFirestoreDocument;
isAdmin: boolean;
battery: string;
}