loopback/lib/middleware/token.js

33 lines
686 B
JavaScript
Raw Normal View History

2013-11-13 19:49:08 +00:00
/**
* Module dependencies.
*/
var loopback = require('../loopback');
var RemoteObjects = require('strong-remoting');
/**
* Export the middleware.
*/
module.exports = token;
function token(app, options) {
options = options || {};
2013-11-14 21:01:47 +00:00
var tokenModelName = options.tokenModelName || 'AccessToken';
var TokenModel = options.model;
assert(TokenModel, 'loopback.token() middleware requires a AccessToken model');
2013-11-13 19:49:08 +00:00
return function (req, res, next) {
2013-11-14 21:01:47 +00:00
TokenModel.findForRequest(req, options, function(err, token) {
if(err) return next(err);
if(token) {
req.accessToken = token;
next();
} else {
return next();
}
});
2013-11-13 19:49:08 +00:00
}
}