Updated auth module
This commit is contained in:
parent
daeb960d9e
commit
c52d38f49b
37
src/app/auth/auth.service.ts
Normal file
37
src/app/auth/auth.service.ts
Normal 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(['/']);
|
||||
}
|
||||
}
|
17
src/app/auth/guards/admin.guard.ts
Normal file
17
src/app/auth/guards/admin.guard.ts
Normal 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));
|
||||
}
|
||||
}
|
17
src/app/auth/guards/guest.guard.ts
Normal file
17
src/app/auth/guards/guest.guard.ts
Normal 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));
|
||||
}
|
||||
}
|
17
src/app/auth/guards/login.guard.ts
Normal file
17
src/app/auth/guards/login.guard.ts
Normal 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
9
src/app/auth/user.ts
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user