https://github.com/mosch/express-http-auth
A simple implementaion of http auth for the express.js framework
https://github.com/mosch/express-http-auth
Last synced: over 1 year ago
JSON representation
A simple implementaion of http auth for the express.js framework
- Host: GitHub
- URL: https://github.com/mosch/express-http-auth
- Owner: mosch
- License: mit
- Created: 2012-03-30T09:29:20.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2018-06-04T07:14:38.000Z (about 8 years ago)
- Last Synced: 2025-03-26T16:04:49.219Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://npmjs.org/package/express-http-auth
- Size: 4.88 KB
- Stars: 11
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Express Http Basic Auth
## Installation
$ npm install express-http-auth
## Quick Start
### Per route authorization
To obtain a "per route authorization", you can use the module as middleware. It's up to you to check the information.
var private = require('express-http-auth').realm('Private Area');
app.get('/private_area', private, function(req, res) {
if (req.username == 'Foo' && req.password == 'Bar') {
} else {
res.send(403);
}
});
To not repeat yourself, you better write a authorization middleware.
var realm = require('express-http-auth').realm('Private Area');
var checkUser = function(req, res, next) {
if (req.username == 'Foo' && req.password == 'Bar') {
next();
} else {
res.send(403);
}
}
var private = [realm, checkUser];
app.get('/private_area', private, function(req, res) {
// your normal code
res.send('Hello '+req.username);
});
app.get('/protected_area', private, function(req, res) {
// your normal code
…
});
## Global authorization
Enables http basic authorization for the express framework.
var http_auth = require('express-http-auth');
// Configuration
app.configure(function() {
// Require http auth
app.use(http_auth.realm('Private Area'));
…
}
Express will now ask for authorization on every request. The authorization informations will be available in the request object.