Files
freeplanet_serverside/server/middleware/authenticate.js

35 lines
918 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');
2018-12-24 20:31:02 +01:00
var authenticate = (req, res, next) => {
var token = req.header('x-auth');
const useragent = req.get('User-Agent');
const access = 'auth ' + useragent;
tools.mylog("TOKEN = ", token);
tools.mylog("USER-AGENT = ", useragent);
2018-12-24 20:31:02 +01:00
User.findByToken(token, access).then((user) => {
2018-12-24 20:31:02 +01:00
if (!user) {
tools.mylog("TOKEN NOT FOUND! Maybe Connected to other Page");
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};