CORS - Facebook - Passport

I am trying to embed OAUTH login via Facebook in my Nodejs / Angular / Express / Passport application, but I am afraid with it.

I still get the CORS error:

XMLHttpRequest is blocked by the CORS policy: the header "Access-Control-Allow-Origin" is present on the requested resource. Therefore, the original https://www.xxxxxx.net 'is not allowed.

Although I have already added to my EXPRESS ROUTER:

router.all('/*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');

    if ('OPTIONS' === req.method) {
      res.send(200);
    }
    else {
      next();
    }
});

In the developer console, I see that the header for the GET call "oauth / facebook" adds "Access-Control-Allow-Origin", etc.

There is no "Access-Control-Allow-Origin" in the callback, etc. - it is right?

router.get('/oauth/facebook/',passport.authenticate('facebook',{
      failureRedirect: '/info',
      scope:['email']
  }));

router.get('/oauth/facebook/callback/', passport.authenticate('facebook',{
      failureRedirect: '/info',
      successRedirect: '/',
      scope:['email']
  }),
  function(req,res){
    if(req.user){
      return res.json({token: req.user.generateJWT()});
    } else {
      return res.status(400).json({message:"Not found"});
    }
});
+4
2

, .

"/oauth/facebook/" href:

<a href="/oauth/facebook/" class="btn btn-primary"><span class="fa fa-facebook"></span> Login with Facebook</a>

, angular .

:   router.get( '/OAuth/facebook/', passport.authenticate( 'facebook', {         failRedirect: '/#!/home',         : [ ' ']     }));

:

router.get('/oauth/facebook/callback/', passport.authenticate('facebook',{
      failureRedirect: '/#!/info',
      scope:['email']
  }),
  function(req,res){
    if(req.user){
      return res.redirect(303, '/#!/fb/' +req.user.generateJWT());
    } else {
      return res.status(400).json({message:"Not found"});
    }
});

: "FB" angular, angular .

+4

Cross-Origin (CORS) , .

index.js: ()

const cors = require('cors');
..
..
app.use(cors());

cors: npm cors

+2

Source: https://habr.com/ru/post/1668627/


All Articles