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;
|
|
|
|
|
2013-11-14 23:27:36 +00:00
|
|
|
function token(options) {
|
2013-11-13 19:49:08 +00:00
|
|
|
options = options || {};
|
2013-11-14 21:01:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|