2017-11-16 16:45:01 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const User = require('../user');
|
|
|
|
const AuthenticationRequired = require('../authentication-required');
|
|
|
|
|
2019-03-03 11:41:56 +01:00
|
|
|
function unsecureAuthentication(basePath = '', app) {
|
|
|
|
app.post(`${basePath}/api/admin/login`, (req, res) => {
|
2017-11-16 16:45:01 +01:00
|
|
|
const user = req.body;
|
|
|
|
req.session.user = new User({ email: user.email });
|
2018-11-22 20:47:06 +01:00
|
|
|
res.status(200)
|
2017-11-16 16:45:01 +01:00
|
|
|
.json(req.session.user)
|
|
|
|
.end();
|
|
|
|
});
|
|
|
|
|
2019-03-03 11:41:56 +01:00
|
|
|
app.use(`${basePath}/api/admin/`, (req, res, next) => {
|
2017-11-16 16:45:01 +01:00
|
|
|
if (req.session.user && req.session.user.email) {
|
|
|
|
req.user = req.session.user;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2019-03-03 11:41:56 +01:00
|
|
|
app.use(`${basePath}/api/admin/`, (req, res, next) => {
|
2017-11-16 16:45:01 +01:00
|
|
|
if (req.user) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
return res
|
|
|
|
.status('401')
|
|
|
|
.json(
|
|
|
|
new AuthenticationRequired({
|
2019-03-03 11:41:56 +01:00
|
|
|
path: `${basePath}/api/admin/login`,
|
2017-11-16 16:45:01 +01:00
|
|
|
type: 'unsecure',
|
|
|
|
message:
|
2018-01-16 15:32:39 +01:00
|
|
|
'You have to indentify yourself in order to use Unleash.',
|
2017-11-16 16:45:01 +01:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
// Updates active sessions every hour
|
|
|
|
req.session.nowInHours = Math.floor(Date.now() / 3600e3);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = unsecureAuthentication;
|