-
• #2
You mean like this? https://developer.mozilla.org/en-US/docsĀ/Web/HTTP/Authentication
Yes, it should be pretty easy. Looks like all you're really doing is reading and setting headers on the HTTP response
-
• #3
Ok thank you.
-
• #4
For people who are interested here is an example:
First you look if the client is already logged in.
If not you send a Header with Status 401 and WWW-Authenticate.// The header you check is "Basic Username:Password". if(req.headers.Authorization != "Basic YWRtaW46YWRtaW4=") { //Between the "" you can write a message that should than be displayed. res.writeHead(401, { 'WWW-Authenticate': 'Basic realm=""' }); res.end(); } else { //If logged in }
Edit:
Here is a little function.
function basicAuth(req, res, username, password, message, callback) { var credentials = btoa(username + ':' + password); if(req.headers.Authorization != "Basic " + credentials) { res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="' + message + '"' }); res.write("401 Unauthorized"); res.end(); } else { callback(); } }
-
• #5
Thanks!
I want to make a basic http auth in my webserver. I am using the ESP8266 Wifi.