javascript - Passport Facebook Strategy, trigger route with AJAX -
the problem: xmlhttprequest cannot load https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…lhost%3a8080%2fapi%2fauth%2ffacebook%2fcallback&client_id=1527429390857121. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:8080' therefore not allowed access.
if click link in error autenticated , datas in json. if remove clientside js router everithing works fine. trying build spa app.
i don't use specific frontend framework, use flatiron director router , generate view handlebars.
on clientside:
var facebook = function () { console.log('get /auth/facebook'); $.ajax({ url: '/api/auth/facebook', type: 'get', datatype: 'jsonp', cache: false }); }; // routes =============================== var routes = { ... '/auth': { '/facebook' : facebook, '/twitter' : twitter, '/google': google }, ... };
on serverside:
// send facebook authentication router.get('/auth/facebook', passport.authenticate('facebook')); // handle callback after facebook has authenticated user router.get('/auth/facebook/callback', function(req, res, next) { passport.authenticate('facebook', function (err, user, info) { if (err) return next(err); if (!user) return res.status(403).json(info); req.login(user, function (err) { if (err) { return next(err); } return res.json({user: user, message: info}); }); })(req, res, next); });
in server.js
... app.use('/api', router); ...
it seems cors issue:
try following:
$ npm install cors
then, in server.js:
var cors = require('cors'); ... app.use(cors());
for more details on cors module: https://www.npmjs.com/package/cors
and on cors , ajax: http://www.bennadel.com/blog/2327-cross-origin-resource-sharing-cors-ajax-requests-between-jquery-and-node-js.htm
Comments
Post a Comment