Skip to content

Instantly share code, notes, and snippets.

@javebratt
Created May 19, 2017 21:22
Show Gist options
  • Save javebratt/46b356ee4b637daa830c019a855aa45a to your computer and use it in GitHub Desktop.
Save javebratt/46b356ee4b637daa830c019a855aa45a to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import firebase from 'firebase';
@Injectable()
export class ProfileProvider {
public userProfileRef;
constructor() {
this.userProfileRef = firebase.database().ref('/userProfile');
}
getUserProfile(): Promise<any> {
return new Promise( (resolve, reject) => {
this.userProfileRef.child(firebase.auth().currentUser.uid)
.on('value', (data) => {
resolve(data.val());
});
});
}
updateName(firstName: string, lastName: string): firebase.Promise<any> {
return this.userProfileRef.child(firebase.auth().currentUser.uid)
.update({
firstName: firstName,
lastName: lastName,
});
}
updateDOB(birthDate: string): firebase.Promise<any> {
return this.userProfileRef.child(firebase.auth().currentUser.uid)
.update({
birthDate: birthDate,
});
}
updateEmail(newEmail: string, password: string): firebase.Promise<any> {
const credential = firebase.auth.EmailAuthProvider
.credential(firebase.auth().currentUser.email, password);
return firebase.auth().currentUser.reauthenticate(credential).then( user => {
firebase.auth().currentUser.updateEmail(newEmail).then( user => {
this.userProfileRef.child(firebase.auth().currentUser.uid)
.update({ email: newEmail });
});
});
}
updatePassword(newPassword: string, oldPassword: string): firebase.Promise<any> {
const credential = firebase.auth.EmailAuthProvider
.credential(firebase.auth().currentUser.email, oldPassword);
return firebase.auth().currentUser.reauthenticate(credential).then( user => {
firebase.auth().currentUser.updatePassword(newPassword).then( user => {
console.log("Password Changed");
}, error => {
console.log(error);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment