javascript - How to pass JSON Web Token w/Passport-Facebook from backend to Angular Controller -


thanks in advance reading - have spent hours trying figure out , has stumped programmers far more knowledgeable , experienced myself, here am. have 2 separate puzzling problems here depending on how make $http request, i'll describe both.

have built app skeleton , decided tackle auth first, , i'm using passport-facebook log user in.

i have view such:

click <div ng-click="vm.login()">here</div> login 

with login function in angular controller as... (just trying data there now):

vm.login = function() {  authservice.login().success(function(data){   console.log(data)  }).error(function(error){   console.log('error ', error)  }) } 

which routes authservice:

auth.savetoken = function(token) {  $window.localstorage['pong-token'] = token; };  auth.login = function() {  return $http.get('/login/facebook')  .success(function(data){    auth.savetoken(data.token);    console.log('token saved');  }).error(function(response){    return(response)  }) }; 

and then, relevant code on backend:

passport.use('facebook', new facebookstrategy({     clientid : process.env.fb_app_id,     clientsecret : process.env.fb_app_secret,     callbackurl : 'http://localhost:3000/login/facebook/callback',     profilefields: ['id', 'email', 'displayname', 'photos', 'first_name',          'last_name', 'hometown', 'link']   },    function(accesstoken, refreshtoken, profile, done) {     var email = profile.emails[0].value;     var username = email.split('@')[0];     knex('users').first().where('email', email).then(function(user){       if (!user) {         knex('users').insert({           email: email,           username: username,           first_name: profile._json.first_name,           last_name: profile._json.last_name,           facebookid: profile.id         }, '*').then(function(user){            user[0].token = jwt.sign({             _id: user[0]._id,             username: user[0].username           }, process.env.session_key);            return done(null, user[0])         });        } else {          facebookid = profile.id;         user.token = jwt.sign({           _id: user._id,           username: user.username         }, process.env.session_key);          return done(null, user)       }     })   } ));  app.get('/login/facebook',   passport.authenticate('facebook', { scope: ['public_profile', 'email',           'user_hometown']}) );  app.get('/login/facebook/callback',   passport.authenticate('facebook', { failureredirect: '/login' }),   function(req, res) {    var token = req.user.token;    console.log('token ', token);    res.json({token: token}) }); 

so generate json web token within passport's facebook strategy, attach user, , token makes facebook login callback -- know works as succeeds in being printed console after request made, seen here along logged response:

token  eyjhbgcioijiuzi1niisinr5cci6ikpxvcj9.eyj1c2vybmftzsi6im1py2hhzwwucy5kdwluiiwiawf0ijoxndgymzk4mde4fq.srujmzja38rusa3bzhgmkyha7fuexgdwaotitgwburo /login/facebook/callback?code=aqbbdg80jgwwocrexq5856bvgdlkgzue1sfioiw3nwklhj18rtakb3kx53hibres5natob9vsft0os1lyfl0cpi22u2drs0kqnce931vjnharvrmj0xq1zb7s7yurerxwe-bjczgvbqmir6cke4o7rkmdm-dah06po4gwhioetgsoyakpbj8njngt75tnzcoiz1dvda9yyasgnkfcu79ezjh3vxwxy26hul-metw6uvv7rdxtu5tqc5jz4cwqa90myi6tuh6jiirzjs0ijy9nx579rc8tnc1loo2dot01jeqwv1sfxiogue7b3v_rdi0hubsd8kngwx-ytybtgf2bq28ulxrrvjac_ltazqzvrcr9g 200 337.450 ms - 155 

...and try , send token authservice json.

issue #1: reason, getting cors error in browser shown here:

xmlhttprequest cannot load https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…ack&scope=public_profile%2cemail%2cuser_hometown&client_id=318885255179710. redirect 'https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…ack&scope=public_profile%2cemail%2cuser_hometown&client_id=318885255179710' 'https://www.facebook.com/login.php?skip_api_login=1&api_key=318885255179710…_&display=page&locale=en_us&logger_id=59aa9608-5d12-49b7-b063-8747be653898' has been blocked cors policy: no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:3000' therefore not allowed access. 

and null value returned controller:

auth.controller.js:14 error  null 

why happening?? making login request facebook server backend. why appear if request coming browser?

issue #2: when attempted fix using jsonp within service this:

auth.login = function(user) {     return $http.jsonp('/login/facebook')     .success(function(data){       auth.savetoken(data.token);     }).error(function(response){       return(response)     })   } 

...i equally bizarre syntax error on client side, in full callback url returned browser ':1' added end ... , undefined value returned controller.

uncaught syntaxerror: unexpected token : callback?code=aqbbdg80jgwwocrexq5856bvgdlkgzue1sfioiw3nwklhj18rtakb3kx53hibres5natob9vsft0os1lyfl0c…:1 auth.controller.js:14 error  undefined 

can decipher madness going on here? potentially worth noting:

  • i consulted question (angular/node/express/passport cross domain problems - enable cors passport facebook authentication) re: cors issues, turning login link done in responses not work me trying json web token client.

  • i have similar passport-auth/json web token structure in app, , works ... app using passport-local rather passport-facebook, , in app, make post request rather client side, since need send along user login data. no such data needs sent along in instance because facebook handling that, , seemed way go. not sure if relevant i'm stumped otherwise.

thanks if made way through ... let me know if can spot i've gone astray! cheers.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -