Hi everybody I have searching hours around and tried everything such like BehaviorSubject or ReplaySube or with Events but unsuccessfull ..
Problem
I have a Profile Page in which is a button loginFb()
which calls my users.services. This Service calls data with FacebookAPI and send them to database.. NOW it would be cool to send back a "callback" to my component with user object :)
Because when I (click) on login, I see the new User in Firebase but my page is not "refreshed", I think it's because my async is too late.. ?
Here is my profile.ts (ProfilePage)
import { Users } from '../../services/users.service';
export class ProfilePage {
user :any;
isConnected :boolean = false;
constructor(public navCtrl: NavController,
public usersService: Users,
public modalCtrl: ModalController) { }
loginFB(){
this.usersService.registerUserFb();
this.usersService.getUserFacebook().subscribe((userObject) => {
this.user = userObject;
})
}
}
And here my users.services.ts
getUserFacebookId(){
Facebook.getLoginStatus().then((response) => {
alert("id in getUSERFACEBOOKID = " + JSON.stringify(response.authResponse.userID));
return response.authResponse.userID;
});
}
getUserFacebook(){
return this.af.database.object('users/' + this.getUserFacebookId());
}
registerUserFb(){
Facebook.login(['email']).then((response) => {
Facebook.getLoginStatus().then((response) => {
if(response.status == 'connected'){
this.checkIfUserExists(response.authResponse.userID);
}
});
});
}
checkIfUserExists(uid) {
let user = this.af.database.object('users/' + uid, { preserveSnapshot: true });
user.subscribe(snapshot => {
if(!snapshot.exists()) {
this.saveInDatabase(uid);
}
});
}
saveInDatabase(uid){
Facebook.api('/' + uid +
'?fields=id, name, ...])
.then((data) => {
var newUser = {
//data saving
};
this.af.database.object('users/' + uid).set(newUser);
this.showToast("User successfully registered");
});
}