javascript - Check if user exists and store his data with Firebase+Angular+Facebook -
i have facebook signup method , want check if user persists in firebase. if is, i'd set data $rootscope.loggedinuser share across controllers.
here's implementation of idea. problem — no data written firebase no redirection performed. seems miss obvious. 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); }); }
Comments
Post a Comment