-
-
Save joseadrian/88c0ad44bb068d8f6e2a3d4f69b3c81c to your computer and use it in GitHub Desktop.
HTTP Basic Authentication with Express 4 using the http-auth module
This file contains 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
var express = require('express'), | |
auth = require('http-auth'); | |
// Configure basic auth | |
var basic = auth.basic({ | |
realm: 'SUPER SECRET STUFF' | |
}, function(username, password, callback) { | |
callback(username == 'admin' && password == 'f00lpr00f'); | |
}); | |
// Set up express app | |
var app = express(); | |
// Create middleware that can be used to protect routes with basic auth | |
var authMiddleware = auth.connect(basic); | |
// Configure an unprotected route | |
app.get('/', function(request, response) { | |
response.send('This is the secret route: <a href="/secret">shhhhhhh!</a>'); | |
}); | |
// Protected route | |
app.get('/secret', authMiddleware, function(request, response) { | |
response.send('you made it!'); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment