1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-23 20:07:40 +02:00
unleash.unleash/src/lib/middleware/demo-authentication.js
2021-04-29 09:57:45 +02:00

39 lines
1.1 KiB
JavaScript

const AuthenticationRequired = require('../types/authentication-required');
function demoAuthentication(app, basePath = '', { userService }) {
app.post(`${basePath}/api/admin/login`, async (req, res) => {
const { email } = req.body;
const user = await userService.loginUserWithoutPassword(email, true);
req.session.user = user;
res.status(200)
.json(req.session.user)
.end();
});
app.use(`${basePath}/api/admin/`, (req, res, next) => {
if (req.session.user && req.session.user.email) {
req.user = req.session.user;
}
next();
});
app.use(`${basePath}/api/admin/`, (req, res, next) => {
if (req.user) {
return next();
}
return res
.status('401')
.json(
new AuthenticationRequired({
path: `${basePath}/api/admin/login`,
type: 'demo',
message:
'You have to identify yourself in order to use Unleash.',
}),
)
.end();
});
}
module.exports = demoAuthentication;