Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Created May 26, 2014 13:48
Show Gist options
  • Save kwhinnery/eefd33883e49cde32854 to your computer and use it in GitHub Desktop.
Save kwhinnery/eefd33883e49cde32854 to your computer and use it in GitHub Desktop.
HTTP Basic Authentication with Express 4 using the http-auth module
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);
@sekcompsci
Copy link

If you want to log out?

@Juni4567
Copy link

Gives the following error:

TypeError: auth.connect is not a function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment