-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-auth-hook.js
More file actions
33 lines (24 loc) · 881 Bytes
/
basic-auth-hook.js
File metadata and controls
33 lines (24 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
const auth = require('basic-auth');
const { User } = require('unleash-server');
const [ username, password ] = process.env.CREDENTIALS.split(':');
function basicAuthentication(app) {
app.use('/api/admin/', (req, res, next) => {
const credentials = auth(req);
if (!credentials || credentials.name !== username || credentials.pass !== password) {
return res
.status('401')
.set({ 'WWW-Authenticate': 'Basic realm="unleash"' })
.end('access denied');
}
const user = new User({ email: `${credentials.name}@webinterpret.com` });
req.user = user;
next();
});
app.use((req, res, next) => {
// Updates active sessions every hour
req.session.nowInHours = Math.floor(Date.now() / 3600e3);
next();
});
}
module.exports = basicAuthentication;