import { Component , OnInit } from '@angular/core' ; import firebase from 'firebase' ; import { MatSnackBar } from '@angular/material/snack-bar' ; import { Router } from '@angular/router' ; import { DataService } from '../data/data.service' ; @ Component ( { selector : 'ff-user' , templateUrl : './user.component.html' , styleUrls : [ './user.component.scss' ] } ) export class UserComponent implements OnInit { email ; password ; isLoggedIn = false ; message ; constructor ( public snackBar , private data , private router ) { } ngOnInit ( ) { if ( ! this . data . connected ) { const strConfig = localStorage . getItem ( 'fd-config' ) ; if ( strConfig !== null && strConfig !== undefined ) { const config = JSON . parse ( strConfig ) ; this . data . connect ( config ) ; } else { this . router . navigate ( [ 'connect' ] ) ; } } firebase . auth ( ) . onAuthStateChanged ( usr => { if ( usr ) { this . data . user = usr ; this . data . connected = true ; this . isLoggedIn = true ; } else { this . isLoggedIn = false ; } } ) ; } signIn ( ) { firebase . auth ( ) . signInWithEmailAndPassword ( this . email , this . password ) . then ( data => { this . data . user = data . user ; this . router . navigate ( [ 'drill' ] ) ; } ) . catch ( ( error ) => { this . snackBar . open ( error . message , undefined , { duration : 10000 , } ) ; } ) ; } signOut ( ) { firebase . auth ( ) . signOut ( ) . then ( ( ) => { } ) . catch ( function ( error ) { } ) ; } get user ( ) { return this . data . user ; } googleSignIn ( ) { const provider = new firebase . auth . GoogleAuthProvider ( ) ; firebase . auth ( ) . signInWithPopup ( provider ) . then ( ( result ) => { this . data . user = result . user ; this . router . navigate ( [ 'drill' ] ) ; } ) . catch ( ( error ) => { this . snackBar . open ( error . message , undefined , { duration : 10000 , } ) ; } ) ; } }