Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Created August 17, 2013 04:44
Show Gist options
  • Save mikermcneil/6255295 to your computer and use it in GitHub Desktop.
Save mikermcneil/6255295 to your computer and use it in GitHub Desktop.
Using custom express middleware in Sails.js circa v0.9.3

Custom middleware is a legacy option, since most times you just want to use policies.

HOWEVER! There are times you want to use sails for quick/dirty things you would normally use express for (but you already have sails around, w/e). More pertinently, if you want middleware to run before the static files from your assets folder are served, policies won't let you do that.

Here's an example of using the BasicAuth middleware for everything:
// Put this in `config/express.js`
module.exports.express = {
  customMiddleware: function (app) {
    app.use(require('../node_modules/sails/node_modules/express').basicAuth('balderdash', 'wickywocky'));
  }
};

Keep in mind this technique only works with Express (and your static files are only available that way anyhow)

@kkotak
Copy link

kkotak commented Jan 15, 2014

Scratch that Mike. I didn't have express installed. Now, I get past this issue and stuck at the passport Verify handler for Google Oauth never being called issue. The search continues :)

_K

@parnurzeal
Copy link

@kkotak @mikermcneil I wonder as _K did by putting passport.initialize on a new file under config directory.
Is that a preferable and clean way for sails? Should it have any other way to initialize passport in api directory, instead?

@geilt
Copy link

geilt commented Aug 26, 2014

For new .10 Sails JS use http.
module.exports = {
http: {
customMiddleware: function(app) {
console.log('Express middleware for passport');
app.use(passport.initialize());
app.use(passport.session());
}
}
};

@adonespitogo
Copy link

I'm new to sails and node.js and I don't know if I'm doing it the sails way. I've defined my middleware configuration for passport like this:

// Location: /config/passport.js
var passport    = require('passport'),
  LocalStrategy = require('passport-local').Strategy,
  bcrypt = require('bcrypt');

passport.serializeUser(function(user, done) {
  done(null, user[0].id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function (err, user) {
    done(err, user);
  });
});

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.find({username: username}).exec(function(err, user) {
      if (err) { return done(null, err); }
      if (!user || user.length < 1) { return done(null, false, { message: 'Incorrect User'}); }
      bcrypt.compare(password, user[0].password, function(err, res) {
        if (!res) return done(null, false, { message: 'Invalid Password'});
        return done(null, user);
      });
    });
  })
);

module.exports = {
  init: function(app){
    app.use(passport.initialize());
    app.use(passport.session());
  }
}
// Location: /config/express.js
module.exports.express = {
  customMiddleware: function(app){
    require('./passport.js').init(app);
  }
}

It works fine for me but I don't know if it follows proper conventions. Sails version is 0.10.5.

@Palash2368
Copy link

Palash2368 commented Feb 2, 2019

I am sending
type : "POST",
data: "a",
contentType : "application/x-www-form-urlencoded;charset=ISO-8859-15",
dataType : 'json',
ajax request but I am getting
{ "a": ""} in my back-end in sails.
Since I'm new to sails I have problem regarding writing middlewares.
Please help me in configuring "config/http.js" regarding my ajax requests.
Thanks in advance@mikermcneil

@saroj990
Copy link

Express is not found in ../node_modules/sails/node_modules/express

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment