import { Injectable } from '@angular/core'; import { Config } from 'ionic-angular'; import { Storage } from '@ionic/storage'; import 'rxjs/add/operator/map'; import * as firebase from 'firebase/app'; // singlenton import { UserService } from '../providers/user/user'; /* Generated class for the AuthService provider. See https://angular.io/docs/ts/latest/guide/dependency-injection.html for more info on providers and Angular 2 DI. */ @Injectable() /** * DESC PROVIDER */ export class AuthService { tenant: string; public token: any; public fireAuth: firebase.auth.Auth; public userProfile: firebase.database.Reference; public urlNodeFirebase: string; constructor( //private afAuth: AngularFireAuth, private config: Config, private storage: Storage, private userService:UserService ) { console.log('Hello AuthService Provider'); // recupero tenant // console.log('ionViewDidLoad First'); // this.userProvider.log(); // log First singleton data // this.userProvider.set("First singleton data") let appConfig = config.get("appConfig"); this.tenant = appConfig.tenant; this.fireAuth = firebase.auth(); this.urlNodeFirebase = '/apps/'+this.tenant+'/contacts/'; this.userProfile = firebase.database().ref(this.urlNodeFirebase); //this.afAuth.authState.subscribe(); // this.afAuth.authState.subscribe((user: firebase.User) => { // this.currentUser = user; // }); } //Start Firebase Auth// // GetUser getUser(): firebase.User { return this.fireAuth.currentUser; } // Create User Anonymous // createAnonymousUser(): firebase.Promise { // return this.fireAuth.signInAnonymously(); // } // Login with Email doLoginFirebase(email: string, password: string): any { return this.fireAuth.signInWithEmailAndPassword(email, password) // .then(res => { // console.log("signInWithEmailAndPassword::: ",res); // }); } // Signin with Facebook // signInWithFacebook(): any { // return this.afAuth.auth.signInWithPopup(new firebase.auth.FacebookAuthProvider()) // .then(res => console.log(res)); // } // Register User with Email register(email: string, password: string, firstname: string, lastname: string): any { return this.fireAuth.createUserWithEmailAndPassword(email, password); } // delete account delete(){ var user = firebase.auth().currentUser; user.delete().then(function() { // User deleted. console.log("delete OK "); }).catch(function(error) { // An error happened. console.log("delete with error: ",error); }); } // Reset Password resetPassword(email: string): any { return this.fireAuth.sendPasswordResetEmail(email); } logoutUser() { console.log("AuthService::logoutUser") return firebase.auth().signOut() //return this.afAuth.auth.signOut(); // .then((res) => { // console.log("logout1",res); // console.log("logout2", this.getUser()); // }) // .catch(function(error) { // console.log("logout failed: " + error.message) // }); } //End Firebase Auth// }