Last active
December 20, 2017 07:41
-
-
Save ivarconr/7905b1da5ec5d52129479d7f517c409a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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