• I want to make a basic http auth in my webserver. I am using the ESP8266 Wifi.

  • 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

  • Ok thank you.

  • 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();
        }
    }
    
  • Thanks!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Is there a way to make a Basic Auth with the Webserver?

Posted by Avatar for Julian1 @Julian1

Actions