I'm building out an API using express.js
(its my first time doing this)
I want to authenticate requests by using the standard auth portion of the url. I want requests to come in as https:// {public-key}:{private-key}@host:port/path
I can't find the auth portion of the url anywhere. req.url is just /path
I found this How to get the full url in express.js? which said to do the following:
req.protocol + "://" + req.get('host') + req.url
But that returns only https:// host:port/path
Any help would be great.
As a side note, if this isn't the standard way to authenticate APIs please let me know!
You can use express.basicAuth() middleware to get the username and password sent with the URL.
app.get('/',
express.basicAuth(function(username, password){
console.log(username);
console.log(password);
return (true);
}),
function(req,res){
...
});
Or like the conventional app.use fashion
app.use(express.basicAuth(function(user, pass){
return 'tj' == user & 'wahoo' == pass;
}));