E
E
Egor Knyazhev2017-08-05 12:19:55
JavaScript
Egor Knyazhev, 2017-08-05 12:19:55

How to change http headers in node js using passport.js and express?

In my api, authorization occurs when accessing / oauth / token where
app.use('/oauth/token', oauth2.token);

oauth2.token = [
  passport.authenticate(['basic', 'oauth2-client-password'], { session: false }),
  aserver.token(),
  aserver.errorHandler()
];

But this way I don't have access to the headers, I need to fix them for cors.
In other methods, this is the case, here there is access to headers through req and res:
app.get('/api/someMethod', function (req, res) 
{
  sendCors(res, req);
  ...
});

Accordingly, the authorization method must be brought to such that I can have access to the headers. But I don’t understand at all how to change this use into post or get so that authorization works.
Tried like this:
app.use('/oauth/token', function (req, res) 
{
  sendCors(res, req);
  oauth2.token[0]();
  oauth2.token[1]();
  oauth2.token[2]();
});

And it doesn't work. How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Serezha, 2017-08-05
Ahen @Ahen

Solved by installing a custom callback:

router.route('/login/')

    .post(function(req, res, next){

        passport.authenticate('digest', function(err, user, info){

            if(err) return console.log(err);

            if(!user){
                res.set('WWW-Authenticate', 'x'+info);
                return res.send(401);
            }

            req.login(user, function(err){
                if(err) return console.log(err);
                res.redirect('/admin/');
            });
        })(req, res, next);
    });

Source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question