lundi 1 décembre 2014

Check if user exists and store his data with Firebase+Angular+Facebook


I have a Facebook signup method and i want it to check if the user is already persists in my Firebase. If it is, i'd like to set this data to $rootScope.loggedInUser to share across controllers.


Here's the implementation of this idea. The problem is — no data written to firebase as well as no redirection performed. It seems that i miss something obvious. Any ideas?


Firebase v2.0.4

AngularFire 0.9.0

Angular 1.2.20



$scope.loginFacebook = function() {
$scope.auth.$authWithOAuthPopup("facebook")
.then(function(authData) {
var userId = authData.uid

function checkIfUserExists(userId) {
(new Firebase(USERS)).child(authData.uid).once('value', function(snapshot) {
var exists = (snapshot.val() !== null);
userExistsCallback(userId, exists);
});
}

function userExistsCallback(exists) {
if (exists) {
var loggedInUser = $firebase(new Firebase(USERS).child(authData.uid)).$asObject();
loggedInUser.$loaded().then(function() {
$rootScope.loggedInUser = loggedInUser;
$window.location.href = '#/profile';
});
} else {
$firebase(new Firebase(USERS).child(authData.uid))
.$update({
"uid": authData.uid,
"email": authData.facebook.email,
"displayName": authData.facebook.displayName,
"image": authData.facebook.cachedUserProfile.picture.data.url
})
.then(function(authData) {
$rootScope.loggedInUser = {
'uid': authData.uid,
'email': authData.facebook.email,
'displayName': authData.facebook.displayName,
'image': authData.facebook.cachedUserProfile.picture.data.url
}
var loggedInUser = $firebase(new Firebase(USERS).child(authData.uid))
$rootScope.loggedInUser = loggedInUser.$asObject();
$window.location.href = '#/profile';
});
}
}
}).catch(function(error) {
console.error("Authentication failed:", error);
});
}




Aucun commentaire:

Enregistrer un commentaire