34 lines
886 B
JavaScript
34 lines
886 B
JavaScript
const server_constants = require('../tools/server_constants');
|
|
|
|
var {User} = require('../models/user');
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
const authenticate = (req, res, next) => {
|
|
const token = req.header('x-auth');
|
|
|
|
// console.log('authenticate... ');
|
|
|
|
const access = 'auth';
|
|
|
|
User.findByToken(token, access).then((user) => {
|
|
if (!user) {
|
|
tools.mylog("TOKEN " + token);
|
|
tools.mylog(" NOT FOUND! (Maybe Connected to other Page) ACCESS: '" + access + "'");
|
|
return Promise.reject(server_constants.RIS_CODE_HTTP_INVALID_TOKEN);
|
|
// res.status().send();
|
|
}
|
|
// tools.mylog('userid', user._id);
|
|
|
|
req.user = user;
|
|
req.token = token;
|
|
req.access = access;
|
|
next();
|
|
}).catch((e) => {
|
|
tools.mylog("ERR =", e);
|
|
res.status(server_constants.RIS_CODE_HTTP_INVALID_TOKEN).send();
|
|
});
|
|
};
|
|
|
|
module.exports = {authenticate};
|