Created
May 26, 2014 13:48
-
-
Save kwhinnery/eefd33883e49cde32854 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); |
Works perfect! Thanks
If you want to log out?
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
I'm using this code but even typing the correct user/pass it keep asking me again.
In my case I want to protect my static files that live on www folder, I'm using this code to achieve this:
Do you know why it's now working properly @kwhinnery?