Created
August 4, 2015 17:47
-
-
Save eshwaryaddanapudi/a9d385557b1b9320f5cb to your computer and use it in GitHub Desktop.
Basic Authentication using Expressjs in Nodejs API with Base64 encoding
This file contains hidden or 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
/* Basic authentication using expressjs in Nodejs applications. | |
The data is sent in the request headers*/ | |
var express = require('express'); | |
var app = express(); | |
app.get('/',function(req,res){ | |
res.send('to go beyond this root for the API, you need to have basic authentication using base64 encoding'); | |
}); | |
app.get('/api/displayNames',function(req,res){ | |
if(req.headers.authorization === undefined){ | |
res.status(401).send('Please provide WWW-Authorization using basic in headers with base 64 encoding'); | |
} | |
else | |
{ | |
//grab the encoded value | |
var encoded = req.headers.authorization.split(' ')[1]; | |
// decode it using base64 | |
var decoded = new Buffer(encoded,'base64').toString(); | |
var name = decoded.split(':')[0]; | |
var password = decoded.split(':')[1]; | |
// check if the passed username and password match with the values in our database. | |
// this is dummy validation. | |
if(name === password){ | |
var namesObj = [{ | |
name: 'Eshwar',place: 'Hyderabad'},{name: 'Prasad',place: 'New York'},{name: 'Yaddanapudi',place: 'San Francisco'}]; | |
res.status(200).send(JSON.stringify(namesObj)); | |
} | |
else{ | |
res.status(403).send('Invalid authorization data provided. Please check username and pwd'); | |
} | |
} | |
}).listen(3423); | |
console.log('waiting at 3423'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment