spacedeck-open/middlewares/setuser.js

36 lines
900 B
JavaScript
Raw Permalink Normal View History

2017-04-07 01:29:05 +02:00
'use strict';
require('../models/schema');
var config = require('config');
module.exports = (req, res, next) => {
const token = req.cookies["sdsession"];
2017-04-07 01:29:05 +02:00
if (token && token != "null" && token !== null) {
User.findOne({
"sessions.token": token
}).populate('team').exec((err, user) => {
if (err) console.error("session.token lookup error:",err);
2017-04-07 01:29:05 +02:00
if (!user) {
res.clearCookie('sdsession');
2017-04-07 01:29:05 +02:00
if (req.accepts("text/html")) {
res.send("Please clear your cookies and try again.");
2017-04-07 01:29:05 +02:00
} else if (req.accepts('application/json')) {
res.status(403).json({
"error": "token_not_found"
});
} else {
res.send("Please clear your cookies and try again.");
2017-04-07 01:29:05 +02:00
}
} else {
req["token"] = token;
req["user"] = user;
next();
}
});
} else {
next();
}
}