File: /var/dev/nowruzgan/rest/config/http.js
module.exports.http = {
middleware: {
order: [
'noCache',
'bodyParser',
'compress',
'detokenizer',
'router',
],
noCache: async (req, res, next) => {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
},
detokenizer: async (req, res, next) => {
let token = req.get('x-token');
if(!token) return next();
let session = await sails.helpers.cache.with({action: 'get', key: `${token}:session`});
if(!session) return next();
if(((new Date()).valueOf() - session.timestamp)/1000 > sails.config.custom.profileCacheTTL){
let user = await User.findOne({id: session.user.id, state: 'active'}).populate('roles');
if(!user){
await sails.helpers.cache.with({action: 'del', key: `${token}:session`});
return next();
}
session.publicUser = User.getPublic(user),
session.user = user,
session.timestamp = (new Date()).valueOf();
await sails.helpers.cache.with({action: 'set', key: `${token}:session`, value: session});
}
req.sessionData = session;
next();
},
},
};