import { Injectable } from '@angular/core' ; import { AngularFireAuth } from '@angular/fire/auth' ; import { from } from 'rxjs' ; import * as firebase from 'firebase/app' ; import { AngularFireDatabase } from '@angular/fire/database' ; import { User } from '../models/user.model' ; @ Injectable ( { providedIn : 'root' } ) export class AuthService { constructor ( private afAuth , private db ) { } register ( email , password ) { return from ( this . afAuth . auth . createUserWithEmailAndPassword ( email , password ) ) ; } updateProfile ( displayName , photoUrl ) { const userProfile = this . afAuth . auth . currentUser ; if ( userProfile ) { return < any > from ( userProfile . updateProfile ( { displayName : displayName , photoURL : photoUrl } ) ) ; } } login ( email , password ) { return from ( this . afAuth . auth . signInWithEmailAndPassword ( email , password ) ) ; } socialLogin ( authProvider ) { let provider ; if ( authProvider === 'google' ) { provider = new firebase . auth . GoogleAuthProvider ( ) ; } if ( authProvider === 'facebook' ) { provider = new firebase . auth . FacebookAuthProvider ( ) ; } if ( authProvider === 'twitter' ) { provider = new firebase . auth . TwitterAuthProvider ( ) ; } return from ( this . afAuth . auth . signInWithPopup ( provider ) ) ; } logout ( uid ) { this . updateOnlineStatus ( uid , false ) ; return from ( this . afAuth . auth . signOut ( ) ) ; } saveUser ( user ) { const users = this . db . object ( 'users/' + user . uid ) ; return users . set ( user ) ; } updateOnlineStatus ( uid , status ) { if ( status ) { this . db . database . ref ( ) . child ( 'users/' + uid ) . onDisconnect ( ) . update ( { isOnline : false } ) ; } return from ( this . db . object ( 'users/' + uid ) . update ( { isOnline : status } ) ) ; } checkUserRole ( uid ) { return this . db . object ( 'admins/' + uid ) . valueChanges ( ) ; } getAuthState ( ) { return this . afAuth . authState ; } getCurrentUser ( ) { return this . afAuth . auth . currentUser ; } }