2021-04-29 09:57:45 +02:00
|
|
|
const AuthenticationRequired = require('../types/authentication-required');
|
2017-11-16 16:45:01 +01:00
|
|
|
|
2021-04-22 10:53:47 +02:00
|
|
|
function demoAuthentication(app, basePath = '', { userService }) {
|
2021-04-09 13:46:53 +02:00
|
|
|
app.post(`${basePath}/api/admin/login`, async (req, res) => {
|
|
|
|
const { email } = req.body;
|
|
|
|
const user = await userService.loginUserWithoutPassword(email, true);
|
|
|
|
req.session.user = user;
|
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) {
|
2020-04-14 22:29:11 +02:00
|
|
|
return next();
|
2017-11-16 16:45:01 +01:00
|
|
|
}
|
2020-04-14 22:29:11 +02:00
|
|
|
return res
|
|
|
|
.status('401')
|
|
|
|
.json(
|
|
|
|
new AuthenticationRequired({
|
|
|
|
path: `${basePath}/api/admin/login`,
|
2021-04-22 10:53:47 +02:00
|
|
|
type: 'demo',
|
2020-04-14 22:29:11 +02:00
|
|
|
message:
|
2021-02-04 12:21:08 +01:00
|
|
|
'You have to identify yourself in order to use Unleash.',
|
2020-04-14 22:29:11 +02:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.end();
|
2017-11-16 16:45:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:53:47 +02:00
|
|
|
module.exports = demoAuthentication;
|