2013-11-13 19:49:08 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var loopback = require('../loopback');
|
|
|
|
var RemoteObjects = require('strong-remoting');
|
2013-12-03 00:37:42 +00:00
|
|
|
var assert = require('assert');
|
2013-11-13 19:49:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the middleware.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = token;
|
|
|
|
|
2013-11-14 23:27:36 +00:00
|
|
|
function token(options) {
|
2013-11-13 19:49:08 +00:00
|
|
|
options = options || {};
|
2013-12-03 01:16:43 +00:00
|
|
|
var TokenModel = options.model || loopback.AccessToken;
|
2013-11-14 21:01:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|