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"];
|
2018-03-30 22:34:27 +02:00
|
|
|
|
2017-04-07 01:29:05 +02:00
|
|
|
if (token && token != "null" && token !== null) {
|
|
|
|
User.findOne({
|
|
|
|
"sessions.token": token
|
|
|
|
}).populate('team').exec((err, user) => {
|
2018-03-30 22:34:27 +02:00
|
|
|
if (err) console.error("session.token lookup error:",err);
|
2017-04-07 01:29:05 +02:00
|
|
|
if (!user) {
|
2018-03-30 22:34:27 +02:00
|
|
|
res.clearCookie('sdsession');
|
2017-04-07 01:29:05 +02:00
|
|
|
|
|
|
|
if (req.accepts("text/html")) {
|
2018-03-30 22:34:27 +02:00
|
|
|
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 {
|
2018-03-30 22:34:27 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|