Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Router } from '@angular/router';
- import { Observable } from 'rxjs';
- import { Injectable } from '@angular/core';
- import { AngularFireAuth } from '@angular/fire/auth';
- import { AngularFirestore } from '@angular/fire/firestore';
- import firebase from 'firebase/app';
- @Injectable({
- providedIn: 'root'
- })
- export class AuthService {
- user$: Observable<any>;
- constructor(private afAuth: AngularFireAuth, public db: AngularFirestore, private router: Router) {
- this.user$ = afAuth.authState;
- this.db = db;
- afAuth.onAuthStateChanged((user) => {
- if (user && user.emailVerified) {
- console.log("Logged in");
- db.collection('user').doc(user.uid).valueChanges().subscribe((data) => {
- console.log("User Data loaded: " + JSON.stringify(data));
- });
- } else {
- this.router.navigate(['/login']);
- }
- });
- }
- async login() {
- await this.afAuth.setPersistence(firebase.auth.Auth.Persistence.SESSION)
- .then(() => {
- const credential = this.afAuth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
- });
- return this.router.navigate(['/dashboard']);
- }
- async logout() {
- await this.afAuth.signOut();
- return this.router.navigate(['/login']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement