2020-04-14 22:29:11 +02:00
|
|
|
/* eslint-disable import/no-unresolved */
|
|
|
|
|
2018-01-04 15:48:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const auth = require('basic-auth');
|
2021-02-12 11:42:00 +01:00
|
|
|
const { User } = require('../dist/lib/server-impl.js');
|
2018-01-04 15:48:48 +01:00
|
|
|
|
|
|
|
function basicAuthentication(app) {
|
|
|
|
app.use('/api/admin/', (req, res, next) => {
|
|
|
|
const credentials = auth(req);
|
|
|
|
|
|
|
|
if (credentials) {
|
|
|
|
// you will need to do some verification of credentials here.
|
|
|
|
const user = new User({ email: `${credentials.name}@domain.com` });
|
|
|
|
req.user = user;
|
2020-04-14 22:29:11 +02:00
|
|
|
return next();
|
2018-01-04 15:48:48 +01:00
|
|
|
}
|
2020-04-14 22:29:11 +02:00
|
|
|
|
|
|
|
return res
|
|
|
|
.status('401')
|
|
|
|
.set({ 'WWW-Authenticate': 'Basic realm="example"' })
|
|
|
|
.end('access denied');
|
2018-01-04 15:48:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = basicAuthentication;
|