Skip to content

Instantly share code, notes, and snippets.

@joseadrian
Forked from kwhinnery/app.js
Created June 18, 2020 03:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joseadrian/88c0ad44bb068d8f6e2a3d4f69b3c81c 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment