Last Updated: September 09, 2016
·
3.377K
· sschepis

Make Passport Work with Restify by fixing redirect functionality with this snippet

Having trouble getting Passport working on Restify? You're not alone. Passport will not cleanly work with Restify due to Restify not implementing the 'redirect' method on the res object; the result is a broken passport implementation with errors thrown whenever /FailureRedirect is invoked to redirect a user upon login failure.

No worries, there's an easy fix - just add your own redirect() implementation like this:

server.use(function(req, res, next) {
    res.redirect = function(addr) {
        res.header('Location', addr);
        res.send(302);
    }
});

Just add this code right after all your server.use calls and that's it! Now Passport will work with your Restify server, and you also get your res.redirect() back. Easy peasy.

2 Responses
Add your response

try this instead:
server.use(function(req, res, next) {
res.redirect = function(addr) {
res.header('Location', addr);
res.send(302);
}
done();
});

Note you're adding the redirect function to each response object, inside a middleware function, so you need to let the middleware keep going.

over 1 year ago ·

Restify actually does have a redirect method. The problem is that it's signature is different than that of Express. Please see https://www.npmjs.com/package/passport-restify for using Passport w/ Restify.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip