https://github.com/survi218/express-basic-auth
Express server to handle basic authentication
https://github.com/survi218/express-basic-auth
base64 basic-authentication express-middleware expressjs mongodb mongoose nodejs-modules rest-api server
Last synced: 3 months ago
JSON representation
Express server to handle basic authentication
- Host: GitHub
- URL: https://github.com/survi218/express-basic-auth
- Owner: survi218
- Created: 2017-06-07T00:49:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-07T01:47:56.000Z (over 8 years ago)
- Last Synced: 2025-03-16T07:16:23.095Z (7 months ago)
- Topics: base64, basic-authentication, express-middleware, expressjs, mongodb, mongoose, nodejs-modules, rest-api, server
- Language: JavaScript
- Homepage:
- Size: 2.04 MB
- Stars: 1
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# express-basic-auth
* Note:In the real-time projects we don't use 'base64' to encode/encrypt the username and password, which is not secured.
* Using basic authentication approach to authenticate users.
- Set up an Express server to handle basic authentication
- Use the basic access authentication approach to do basic authentication.
# Setting up the Express Server
- Create a folder named basic-auth and then create package.json and server.js files.
- Then go to the basic-auth folder and install all the node modules by typing the following at the prompt:````
npm install
````
* Open the server.js file write the code as follows:`````
var express = require('express');
var morgan = require('morgan');
var hostname = 'localhost';
var port = 3000;
var app = express();
app.use(morgan('dev'));
function auth (req, res, next) {
console.log(req.headers);
var authHeader = req.headers.authorization;
if (!authHeader) {
var err = new Error('You are not authenticated!');
err.status = 401;
next(err);
return;
}
var auth = new Buffer(authHeader.split(' ')[1], 'base64').toString().split('
:');
var user = auth[0];
var pass = auth[1];
if (user == 'admin' && pass == 'password') {
next(); // authorized
} else {
var err = new Error('You are not authenticated!');
err.status = 401;
next(err);
}
}
app.use(auth);
app.use(express.static(__dirname + '/public'));
app.use(function(err,req,res,next) {
res.writeHead(err.status || 500, {
'WWW-Authenticate': 'Basic',
'Content-Type': 'text/plain'
});
res.end(err.message);
});
app.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});
`````* Save the changes and start the server. Access the server from a browser and see the result