2023-11-04 13:39:59 +00:00
|
|
|
const {models} = require('vn-loopback/server/server');
|
|
|
|
|
2017-10-13 14:22:45 +00:00
|
|
|
module.exports = function(options) {
|
2023-11-04 13:39:59 +00:00
|
|
|
return async function(req, res, next) {
|
|
|
|
if (!req.accessToken) {
|
|
|
|
const token = req.headers.authorization;
|
|
|
|
if (!token) return next();
|
|
|
|
|
|
|
|
const accessToken = await models.AccessToken.findById(token);
|
|
|
|
if (!accessToken) return next();
|
|
|
|
|
2017-10-13 14:22:45 +00:00
|
|
|
return next();
|
2023-11-04 13:39:59 +00:00
|
|
|
}
|
2017-10-13 14:22:45 +00:00
|
|
|
|
|
|
|
let LoopBackContext = require('loopback-context');
|
|
|
|
let loopbackContext = LoopBackContext.getCurrentContext();
|
|
|
|
|
2019-01-07 07:41:54 +00:00
|
|
|
if (loopbackContext)
|
2017-10-13 14:22:45 +00:00
|
|
|
loopbackContext.set('currentUser', req.accessToken.userId);
|
2019-01-07 07:41:54 +00:00
|
|
|
|
2017-10-13 14:22:45 +00:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|