Hi I have this code which gets and posts facebook data into firebase database, As you see right now the code is repeating itself all the time, I want to create a provider or something similar to make it more effcient, but I don´t know how to start becasuse the facebook credential token token is getting me crazy…
Here is the code:
facebookLogin(): void {
let provider = new firebase.auth.FacebookAuthProvider();
provider.addScope('user_friends');
provider.addScope('user_birthday');
provider.addScope('user_likes');
provider.addScope('user_location');
if(this.platform.is('cordova')) {
this.fire.auth.signInWithRedirect(provider)
.then(() => {
this.fire.auth.getRedirectResult()
.then(res => {
console.log("provider: " + res.user);
this.token = res.credential.accessToken;
this.providerUid = res.user.providerData[0].uid;
let userRef = firebase.database().ref(`/userProfile/${res.user.uid}/facebook/`);
let permissions = "id,email,first_name,last_name,gender,birthday,age_range,locale,location,currency,cover,devices,payment_pricepoints,timezone,is_verified,verified,events,music,likes,friends,friendlists";
let url = `https://graph.facebook.com/v2.10/${this.providerUid}?fields=${permissions}&access_token=${this.token}`;
this.http.get(url).subscribe(response => {
let user = response.json();
userRef.update({
id: user.id,
email: user.email,
firstName: user.first_name,
lastName: user.last_name,
gender: user.gender || null,
birthday: user.birthday || null,
age_range: user.age_range || null,
locale: user.locale || null,
location: user.location || null,
cover: user.cover || null,
devices: user.devices || null,
payment_pricepoints: user.payment_pricepoints || null,
timezone: user.timezone || null,
is_verified: user.is_verified,
verified: user.verified,
events: user.events || null,
music: user.music || null,
likes: user.likes || null,
friends: user.friends || null,
friendlists: user.friendlists || null
});
});
this.navCtrl.setRoot(HomePage);
});
});
} else {
this.fire.auth.signInWithPopup(provider)
.then(res => {
console.log("provider: " + res.user);
this.token = res.credential.accessToken;
this.providerUid = res.user.providerData[0].uid;
let userRef = firebase.database().ref(`/userProfile/${res.user.uid}/facebook/`);
let permissions = "id,email,first_name,last_name,gender,birthday,age_range,locale,location,currency,cover,devices,payment_pricepoints,timezone,is_verified,verified,events,music,likes,friends,friendlists";
let url = `https://graph.facebook.com/v2.10/${this.providerUid}?fields=${permissions}&access_token=${this.token}`;
this.http.get(url).subscribe(response => {
let user = response.json();
userRef.update({
id: user.id,
email: user.email,
firstName: user.first_name,
lastName: user.last_name,
gender: user.gender || null,
birthday: user.birthday || null,
age_range: user.age_range || null,
locale: user.locale || null,
location: user.location || null,
cover: user.cover || null,
devices: user.devices || null,
payment_pricepoints: user.payment_pricepoints || null,
timezone: user.timezone || null,
is_verified: user.is_verified,
verified: user.verified,
events: user.events || null,
music: user.music || null,
likes: user.likes || null,
friends: user.friends || null,
friendlists: user.friendlists || null
});
});
this.navCtrl.setRoot(HomePage);
});
}
}
as you see I have to duplicate the http subscription, so my question is, how van I put this thing in a provider?, and call it without losing the credential token value?
Thanks