Skip to content

Instantly share code, notes, and snippets.

@ivarconr
Last active December 20, 2017 07:41
Show Gist options
  • Save ivarconr/7905b1da5ec5d52129479d7f517c409a to your computer and use it in GitHub Desktop.
Save ivarconr/7905b1da5ec5d52129479d7f517c409a to your computer and use it in GitHub Desktop.
'use strict';
const auth = require('basic-auth');
const User = require('../user');
function basicAuthentication(app) {
app.use('/api/admin/', (req, res, next) => {
const credentials = auth(req);
if (credentials) {
// you will obviusly need to do some clever verification of credentials here.
const user = new User({ email: `${credentials.name}@finn.no` });
req.user = user;
next();
} else {
return res
.status('401')
.set({ 'WWW-Authenticate': 'Basic realm="example"' })
.end('access denied');
}
});
app.use((req, res, next) => {
// Updates active sessions every hour
req.session.nowInHours = Math.floor(Date.now() / 3600e3);
next();
});
}
module.exports = basicAuthentication;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment