Search code examples
node.jseveryauthrestify

Using everyauth with restify


I'm trying to use everyauth to handle authentication for a rest api created with restify. But can't find a starting point. I'd expect to be doing something like:

var restify = require('restify');  
var everyauth = require('everyauth');
var server = restify.createServer();
server.use(everyauth.middleware());

but restify does not accept the everyauth middleware.

How do I go about setting up restify and everyauth?


Solution

  • The issue you are having is restify does not and current will not have a middleware layer.

    The below is from the author of restify

    I've thought about this quite a bit, and the thing that worries me here is signing up for compatibility with connect evermore. I don't have control or input over what they decide to do. This seems more in the vein of "if it works, great".

    I'm going to close this out with a "won't fix" for now :\

    https://github.com/mcavage/node-restify/issues/89

    What you can do is use connect and add the restify server on top of that, then you can use connect to manage your middleware like everyauth.

    Here is a great sample of this, I have it working great on my system as-is.

    // Restify server config here
    var server = restify.createServer({
      name: 'restify-test',
      version: '1.0.0',
    });
    
    // ...
    
    // Connect config here
    var connectApp = connect()
        .use(connect.logger())
        .use(connect.bodyParser())
        .use(connect.query())
        .use(connect.cookieParser())
        // And this is where the magic happens
        .use("/api", function (req, res) {
                 server.server.emit('request', req, res);
             });
    
    connectApp.listen(8080);
    

    https://gist.github.com/2140974

    Then you can add everyauth to connect as per the documents.

    Hope that helps.