loopback/lib/middleware/token.js

32 lines
617 B
JavaScript

/**
* Module dependencies.
*/
var loopback = require('../loopback');
var RemoteObjects = require('strong-remoting');
/**
* Export the middleware.
*/
module.exports = token;
function token(options) {
options = options || {};
var TokenModel = options.model;
assert(TokenModel, 'loopback.token() middleware requires a AccessToken model');
return function (req, res, next) {
TokenModel.findForRequest(req, options, function(err, token) {
if(err) return next(err);
if(token) {
req.accessToken = token;
next();
} else {
return next();
}
});
}
}