Skip to content

Instantly share code, notes, and snippets.

@gusdevops
Created July 3, 2017 12:21
Show Gist options
  • Save gusdevops/fc687a886f1722b02e1c2c18b232ddf4 to your computer and use it in GitHub Desktop.
Save gusdevops/fc687a886f1722b02e1c2c18b232ddf4 to your computer and use it in GitHub Desktop.
authProvider.js
import { Injectable } from '@angular/core';
import { ToastController, Platform } from 'ionic-angular'
import { Facebook } from '@ionic-native/facebook';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from "rxjs/Observable";
import { AngularFireDatabase } from 'angularfire2/database'; // Used by pdfs
import * as firebase from 'firebase/app';
@Injectable()
export class AuthProvider {
// private authState: FirebaseAuthState;
// public onAuth: EventEmitter<FirebaseAuthState> = new EventEmitter();
private toastAddUser: any;
public firebase : any;
private authenticated: boolean;
public displayName : string;
user: Observable<any>;
public userProfile: any;
//PDFs
public dicUrl : string;
public aceUrl : string;
constructor(private firebaseAuth: AngularFireAuth,
private toastCtrl: ToastController,
private platform: Platform,
private facebook: Facebook,
public afDatabase: AngularFireDatabase) {
console.log("Heelo from authProvider");
this.authenticated = false;
this.getPdfUrl();
this.user = firebaseAuth.authState;
firebaseAuth.authState.subscribe(user => {
if (!user) {
this.displayName = null;
return;
}
this.displayName = user.displayName;
});
}
signup(credentials) {
this.firebaseAuth
.auth
.createUserWithEmailAndPassword(credentials.email, credentials.password)
.then(value => {
console.log('Success!', value);
})
.catch(err => {
console.log('Something went wrong:',err.message);
});
}
loginWithFacebook(): any {
if (this.platform.is("core")) {
this.authenticated=true;
return this.firebaseAuth.auth.signInWithPopup(new firebase.auth.FacebookAuthProvider());
} else {
return new Promise<any>((resolve, reject) => {
this.facebook.login(['email']).then( (response) => {
const facebookCredential = firebase.auth.FacebookAuthProvider
.credential(response.authResponse.accessToken);
this.firebaseAuth.auth.signInWithCredential(facebookCredential)
.then((success) => {
this.authenticated = true;
this.userProfile = success;
resolve( {user:this.authenticated} );
});
//.catch((error) => {reject(false);});
}).catch((error) => { alert(error) });
// setTimeout( () => {
// alert("Timeout");
// resolve( {user:this.authenticated} );
// }, 120500);
});
}
}
/**
* This method is to mae a login with a user
*/
loginWithEmail(credentials) {
return Observable.create(observer => {
this.firebaseAuth.auth.signInWithEmailAndPassword(credentials.email,credentials.password).then((authData) => {
//console.log(authData);
this.authenticated = true;
observer.next(authData);
}).catch((error) => {
this.authenticated = false;
observer.error(error);
});
});
}
registerUser(credentials: any) {
return Observable.create(observer => {
this.firebaseAuth.auth.createUserWithEmailAndPassword(credentials.email,credentials.password).then(authData => {
//authData.auth.updateProfile({displayName: credentials.displayName, photoURL: credentials.photoUrl}); //set name and photo
this.toastAddUser.present();
observer.next(authData);
}).catch(error => {
//console.log(error);
observer.error(error);
});
});
}
resetPassword(emailAddress:string){
return Observable.create(observer => {
this.firebaseAuth.auth.sendPasswordResetEmail(emailAddress).then(function(success) {
observer.next(success);
}, function(error) {
observer.error(error);
});
});
}
public getPdfUrl() {
console.log("GET PDFs %%");
this.afDatabase.object('/acessorios', { preserveSnapshot: true }).subscribe(snapshot => {
this.aceUrl = snapshot.val();
});
this.afDatabase.object('/dicionario', { preserveSnapshot: true }).subscribe(snapshot => {
this.dicUrl = snapshot.val();
});
}
public logout(): void {
this.firebaseAuth.auth.signOut();
this.authenticated = false;
}
isAuthenticated(): boolean {
return this.authenticated;
}
currentUser(): void {
// return this.authState?this.authState.auth.email:'';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment