Files
freeplanet_serverside/server/middleware/authenticate.js

34 lines
886 B
JavaScript
Raw Normal View History

const server_constants = require('../tools/server_constants');
2019-02-05 03:40:22 +01:00
var {User} = require('../models/user');
2018-12-24 20:31:02 +01:00
const tools = require('../tools/general');
const authenticate = (req, res, next) => {
const token = req.header('x-auth');
2018-12-24 20:31:02 +01:00
// console.log('authenticate... ');
const access = 'auth';
2018-12-24 20:31:02 +01:00
User.findByToken(token, access).then((user) => {
2018-12-24 20:31:02 +01:00
if (!user) {
2019-02-13 18:47:58 +01:00
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();
2018-12-24 20:31:02 +01:00
}
// tools.mylog('userid', user._id);
2018-12-24 20:31:02 +01:00
req.user = user;
req.token = token;
req.access = access;
2018-12-24 20:31:02 +01:00
next();
}).catch((e) => {
tools.mylog("ERR =", e);
res.status(server_constants.RIS_CODE_HTTP_INVALID_TOKEN).send();
2018-12-24 20:31:02 +01:00
});
};
module.exports = {authenticate};