Skip to content

Commit 9725eeb

Browse files
committed
v3: add securing examples
1 parent 5453ba1 commit 9725eeb

16 files changed

+13474
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const auth = require('basic-auth');
2+
const { User } = require('unleash-server');
3+
4+
function basicAuthentication(app) {
5+
app.use('/api/admin/', (req, res, next) => {
6+
const credentials = auth(req);
7+
8+
if (credentials) {
9+
// you will need to do some verification of credentials here.
10+
const user = new User({ email: `${credentials.name}@domain.com` });
11+
req.user = user;
12+
return next();
13+
}
14+
15+
return res
16+
.status('401')
17+
.set({ 'WWW-Authenticate': 'Basic realm="example"' })
18+
.end('access denied');
19+
});
20+
}
21+
22+
module.exports = basicAuthentication;

v3/securing-basic-auth/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const unleash = require('unleash-server');
2+
3+
const basicAuth = require('./basic-auth-hook');
4+
5+
unleash.start({
6+
databaseUrl: 'postgres://unleash_user:passord@localhost:5432/unleash',
7+
adminAuthentication: 'custom',
8+
preRouterHook: basicAuth,
9+
});

0 commit comments

Comments
 (0)