1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-23 20:07:40 +02:00
unleash.unleash/src/lib/middleware/simple-authentication.js
Fredrik Strand Oseberg c1aab06798
Feature/setup typescript
This sets up the typescript compiler.

Allowing gradual migration to typescript.

Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
Co-authored-by: Fredrik Oseberg <fredrik.oseberg@getunleash.ai>
Co-authored-by: Clint Checkett <clintchecketts@churchofjesuschrist.org>

fixes: #676
2021-02-12 11:42:00 +01:00

45 lines
1.3 KiB
JavaScript

const auth = require('basic-auth');
const User = require('../user');
const AuthenticationRequired = require('../authentication-required');
function unsecureAuthentication(basePath = '', app) {
app.post(`${basePath}/api/admin/login`, (req, res) => {
const user = req.body;
req.session.user = new User({ email: user.email });
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;
} else if (req.header('authorization')) {
const user = auth(req);
if (user && user.name) {
req.user = new User({ username: user.name });
}
}
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: 'unsecure',
message:
'You have to identify yourself in order to use Unleash.',
}),
)
.end();
});
}
module.exports = unsecureAuthentication;