javascript - Angularjs FB login using factory -
i new angularjs.i using factories have written fb login code. , during last step sending data server user registered in database , token sent.
here code.
'use strict' app.factory('authenticationfactory',['env','$http','$rootscope', function (env,$http,$rootscope) { return { sociallogin:function(data){ return $http.post($rootscope.apiurl+'sociallogin',data).then(function (resp) { if(resp.status == 200) { return resp.data; } }) }, fblogin: function () { var fb = window.fb; var scopes = 'public_profile,email'; var = this; fb.login(function (response) { return that.facebookstatuschangecallback(response); }, {scope: scopes}); }, facebookstatuschangecallback: function(response){ if (response.status === 'connected') { // logged app , facebook. var r = this.facebookapirequest(response); console.log(r); } else if (response.status === 'not_authorized') { // person logged facebook, not app. console.log('please log app.'); } else { // person not logged facebook, we're not sure if // logged app or not. console.log('please log facebook.'); } }, facebookapirequest: function (authresponse) { var = this; var r = fb.api('/me?fields=id,name,email,gender,first_name,last_name,age_range,link,birthday', function (response) { var r = fb.api("/" + response.id + "/picture?height=720", function (pictureresponse) { if (pictureresponse && !pictureresponse.error) { /* handle result */ response.profile_pic = pictureresponse.data.url; response.access_token = authresponse.authresponse.accesstoken; response.provider = 'facebook'; response.devicetoken = ''; response.full_name = response.first_name+' '+response.last_name; var r = that.socialpluginlogin(response).then(function (resp) { return that.resp; }); return r; } else { console.log('error while fatching fb pic'); } }); console.log(r); }); console.log(that); }, socialpluginlogin : function (data) { var resp = this.sociallogin(data).then(function (resp) { return resp; }); return resp; } }; }]);
i calling fblogin()
function controller. need response function sociallogin()
can change state.
where going wrong.??
the answer pointing in wrong direction, try:
your function fblogin
should return promise, can resolved sociallogin
later. since fblogin
doesn't return thing, don't receive signal completed login.
see this:
// add $q here app.factory('authenticationfactory',['env','$http','$rootscope','$q', function (env,$http,$rootscope,$q) { var loginpromise; return { sociallogin:function(data){ return $http.post($rootscope.apiurl+'sociallogin',data).then(function (resp) { if(resp.status == 200) { // connection controller loginpromise.resolve(resp.data); return resp.data; } }) }, fblogin: function () { var fb = window.fb; var scopes = 'public_profile,email'; var = this; fb.login(function (response) { return that.facebookstatuschangecallback(response); }, {scope: scopes}); // create , return promise loginpromise = $q.defer(); // edit: fault, return promise: return loginpromise.promise; }, //...
and add controller:
authenticationfactory.fblogin().then(function(data){ // check out: console.dir(data); })
additional things should consider:
- define functions in function body, not in return statement. can eliminate
that=this
way - only return api, not functions
- read on promises, way go in angular world. might use callbacks, tedious handle.
Comments
Post a Comment