express-masquerade

Add it as middleware. The first parameter is a function that gets a user by id:
var express = require('express');
var masquerade = require('express-masquerade');
var app = express();
app.use(masquerade(function(id, next) {
User.find(id).complete(next);
}));
This will set req.user
to the user with an id of 2:
curl localhost:3000/profile --header "x-masquerade-as: 2"
Installation
$ npm install express-masquerade
Options
Pass options as the second parameter:
app.use(require('express-masquerade')(getUser, options));
Use a different header than x-masquerade-as
.
var options = {
header: 'masquerading-as'
};
authorize
(function)
Set a function to authorize whether or not the user has permission to masquerade. Should return true or false.
var options = {
authorize: function(req) {
return req.user.role === 'admin';
}
};