{"id":96364,"date":"2020-04-28T09:21:37","date_gmt":"2020-04-28T09:21:37","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=96364"},"modified":"2025-03-17T12:07:55","modified_gmt":"2025-03-17T12:07:55","slug":"esp32-esp8266-web-server-http-authentication","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-web-server-http-authentication\/","title":{"rendered":"ESP32\/ESP8266 Web Server HTTP Authentication (Username and Password Protected)"},"content":{"rendered":"\n<p>Learn how to add HTTP authentication with username and password to your ESP32 and ESP8266 NodeMCU web server projects using Arduino IDE. You can only access your web server if you type the correct user and pass. If you logout, you can only access again if you enter the right credentials.<\/p>\n\n\n\n<p>The method we&#8217;ll use can be applied to web servers built using the ESPAsyncWebServer library.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Web Server HTTP Authentication Username and Password Protected Arduino IDE\" class=\"wp-image-96381\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>The ESP32\/ESP8266 boards will be programmed using Arduino IDE. So make sure you have these boards installed:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\" title=\"\">Installing ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp8266-nodemcu-arduino-ide-2-0\/\" title=\"\">Installing ESP8266 Board in Arduino IDE (Windows, Mac OS X, Linux)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Security Concerns<\/h2>\n\n\n\n<p>This project is meant to be used in your local network to protect from anyone just typing the ESP IP address and accessing the web server (like unauthorized family member or friend). <\/p>\n\n\n\n<p>If your network is properly secured, running an HTTP server with basic authentication is enough for most applications. If someone has managed to hack your network, it doesn&#8217;t matter if you use HTTP or HTTPS. The hacker can bypass HTTPS and get your user\/pass.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>Let&#8217;s take a quick look at the features of the project we&#8217;ll build.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"903\" height=\"671\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Password-Protected-Web-Server-Overview.png?resize=903%2C671&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Password Protected Web Server Project Overview\" class=\"wp-image-96530\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Password-Protected-Web-Server-Overview.png?w=903&amp;quality=100&amp;strip=all&amp;ssl=1 903w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Password-Protected-Web-Server-Overview.png?resize=300%2C223&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Password-Protected-Web-Server-Overview.png?resize=768%2C571&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 903px) 100vw, 903px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>In this tutorial you&#8217;ll learn how to password protect your web server;<\/li>\n\n\n\n<li>When you try to access the web server page on the ESP IP address, a window pops up asking for a username and password;<\/li>\n\n\n\n<li>To get access to the web server page, you need to enter the right username and password (defined in the ESP32\/ESP8266 sketch);<\/li>\n\n\n\n<li>There&#8217;s a logout button on the web server. If you click the logout button, you&#8217;ll be redirected to a logout page. Then, close all web browser tabs to complete the logout process;<\/li>\n\n\n\n<li>You can only access the web server again if you login with the right credentials;<\/li>\n\n\n\n<li>If you try to access the web server from a different device (on the local network) you also need to login with the right credentials (even if you have a successful login on another device);<\/li>\n\n\n\n<li>The authentication is not encrypted.<\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntcorange\"><strong>Note: <\/strong> this project was tested on Google Chrome and Firefox web browsers and Android devices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Libraries \u2013 Async Web Server<\/h2>\n\n\n\n<p>To build the web server you need to install the following libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ESP32:<\/strong>&nbsp;install the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer<\/a> and the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/AsyncTCP\" target=\"_blank\" rel=\"noopener\" title=\"\">AsyncTCP&nbsp;<\/a>libraries&nbsp;(by ESP32Async).<\/li>\n\n\n\n<li><strong>ESP8266:<\/strong>&nbsp;install the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer<\/a>&nbsp;and the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncTCP\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncTCP<\/a>&nbsp;libraries (by ESP32Async).<\/li>\n<\/ul>\n\n\n\n<p>You can install those libraries in the Arduino IDE Library Manager. Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong> and search for the libraries&#8217; names.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Web Server Code with Authentication<\/h2>\n\n\n\n<p>Copy the following code to your Arduino IDE.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-esp8266-web-server-http-authentication\/\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*********\/\n\n\/\/ Import required libraries\n#ifdef ESP32\n  #include &lt;WiFi.h&gt;\n  #include &lt;AsyncTCP.h&gt;\n#else\n  #include &lt;ESP8266WiFi.h&gt;\n  #include &lt;ESPAsyncTCP.h&gt;\n#endif\n#include &lt;ESPAsyncWebServer.h&gt;\n\n\/\/ Replace with your network credentials\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\nconst char* http_username = &quot;admin&quot;;\nconst char* http_password = &quot;admin&quot;;\n\nconst char* PARAM_INPUT_1 = &quot;state&quot;;\n\nconst int output = 2;\n\n\/\/ Create AsyncWebServer object on port 80\nAsyncWebServer server(80);\n\nconst char index_html[] PROGMEM = R&quot;rawliteral(\n&lt;!DOCTYPE HTML&gt;&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt;ESP Web Server&lt;\/title&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n  &lt;style&gt;\n    html {font-family: Arial; display: inline-block; text-align: center;}\n    h2 {font-size: 2.6rem;}\n    body {max-width: 600px; margin:0px auto; padding-bottom: 10px;}\n    .switch {position: relative; display: inline-block; width: 120px; height: 68px} \n    .switch input {display: none}\n    .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}\n    .slider:before {position: absolute; content: &quot;&quot;; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}\n    input:checked+.slider {background-color: #2196F3}\n    input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;h2&gt;ESP Web Server&lt;\/h2&gt;\n  &lt;button onclick=&quot;logoutButton()&quot;&gt;Logout&lt;\/button&gt;\n  &lt;p&gt;Ouput - GPIO 2 - State &lt;span id=&quot;state&quot;&gt;%STATE%&lt;\/span&gt;&lt;\/p&gt;\n  %BUTTONPLACEHOLDER%\n&lt;script&gt;function toggleCheckbox(element) {\n  var xhr = new XMLHttpRequest();\n  if(element.checked){ \n    xhr.open(&quot;GET&quot;, &quot;\/update?state=1&quot;, true); \n    document.getElementById(&quot;state&quot;).innerHTML = &quot;ON&quot;;  \n  }\n  else { \n    xhr.open(&quot;GET&quot;, &quot;\/update?state=0&quot;, true); \n    document.getElementById(&quot;state&quot;).innerHTML = &quot;OFF&quot;;      \n  }\n  xhr.send();\n}\nfunction logoutButton() {\n  var xhr = new XMLHttpRequest();\n  xhr.open(&quot;GET&quot;, &quot;\/logout&quot;, true);\n  xhr.send();\n  setTimeout(function(){ window.open(&quot;\/logged-out&quot;,&quot;_self&quot;); }, 1000);\n}\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n)rawliteral&quot;;\n\nconst char logout_html[] PROGMEM = R&quot;rawliteral(\n&lt;!DOCTYPE HTML&gt;&lt;html&gt;\n&lt;head&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;p&gt;Logged out or &lt;a href=&quot;\/&quot;&gt;return to homepage&lt;\/a&gt;.&lt;\/p&gt;\n  &lt;p&gt;&lt;strong&gt;Note:&lt;\/strong&gt; close all web browser tabs to complete the logout process.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n)rawliteral&quot;;\n\n\/\/ Replaces placeholder with button section in your web page\nString processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == &quot;BUTTONPLACEHOLDER&quot;){\n    String buttons =&quot;&quot;;\n    String outputStateValue = outputState();\n    buttons+= &quot;&lt;p&gt;&lt;label class=\\&quot;switch\\&quot;&gt;&lt;input type=\\&quot;checkbox\\&quot; onchange=\\&quot;toggleCheckbox(this)\\&quot; id=\\&quot;output\\&quot; &quot; + outputStateValue + &quot;&gt;&lt;span class=\\&quot;slider\\&quot;&gt;&lt;\/span&gt;&lt;\/label&gt;&lt;\/p&gt;&quot;;\n    return buttons;\n  }\n  if (var == &quot;STATE&quot;){\n    if(digitalRead(output)){\n      return &quot;ON&quot;;\n    }\n    else {\n      return &quot;OFF&quot;;\n    }\n  }\n  return String();\n}\n\nString outputState(){\n  if(digitalRead(output)){\n    return &quot;checked&quot;;\n  }\n  else {\n    return &quot;&quot;;\n  }\n  return &quot;&quot;;\n}\n\nvoid setup(){\n  \/\/ Serial port for debugging purposes\n  Serial.begin(115200);\n\n  pinMode(output, OUTPUT);\n  digitalWrite(output, LOW);\n  \n  \/\/ Connect to Wi-Fi\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(1000);\n    Serial.println(&quot;Connecting to WiFi..&quot;);\n  }\n\n  \/\/ Print ESP Local IP Address\n  Serial.println(WiFi.localIP());\n\n  \/\/ Route for root \/ web page\n  server.on(&quot;\/&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    if(!request-&gt;authenticate(http_username, http_password))\n      return request-&gt;requestAuthentication();\n    request-&gt;send(200, &quot;text\/html&quot;, index_html, processor);\n  });\n    \n  server.on(&quot;\/logout&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(401);\n  });\n\n  server.on(&quot;\/logged-out&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/html&quot;, logout_html, processor);\n  });\n\n  \/\/ Send a GET request to &lt;ESP_IP&gt;\/update?state=&lt;inputMessage&gt;\n  server.on(&quot;\/update&quot;, HTTP_GET, [] (AsyncWebServerRequest *request) {\n    if(!request-&gt;authenticate(http_username, http_password))\n      return request-&gt;requestAuthentication();\n    String inputMessage;\n    String inputParam;\n    \/\/ GET input1 value on &lt;ESP_IP&gt;\/update?state=&lt;inputMessage&gt;\n    if (request-&gt;hasParam(PARAM_INPUT_1)) {\n      inputMessage = request-&gt;getParam(PARAM_INPUT_1)-&gt;value();\n      inputParam = PARAM_INPUT_1;\n      digitalWrite(output, inputMessage.toInt());\n    }\n    else {\n      inputMessage = &quot;No message sent&quot;;\n      inputParam = &quot;none&quot;;\n    }\n    Serial.println(inputMessage);\n    request-&gt;send(200, &quot;text\/plain&quot;, &quot;OK&quot;);\n  });\n  \n  \/\/ Start server\n  server.begin();\n}\n  \nvoid loop() {\n  \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Web_Server_HTTP_Auth_2.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You just need to enter your network credentials (SSID and password) and the web server will work straight away. The code is compatible with both the <a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a> boards. <\/p>\n\n\n\n<p>As an example, we&#8217;re building a web server that controls <span class=\"rnthl rntcblue\">GPIO 2<\/span>. You can use the HTTP authentication with any web server built with the <a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer library<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>We\u2019ve already explained in great details how web servers like this work in previous tutorials (<a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide\/\">DHT Temperature Web Server<\/a> or <a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-relay-web-server\/\">Relay Web Server<\/a>), so we\u2019ll just take a look at the relevant parts to add username and password authentication to the web server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Network Credentials<\/h3>\n\n\n\n<p>As mentioned previously, you need to insert your network credentials in the following lines:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Your Username and Password<\/h3>\n\n\n\n<p>In the following variables set the username and password for your web server. By default, the username is <strong>admin<\/strong> and the password is also <strong>admin<\/strong>. We definitely recommend to change them.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* http_username = \"admin\";\nconst char* http_password = \"admin\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Logout Button<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">index_html<\/span> variable you should add some HTML text to add a logout button. In this example, it&#8217;s a simple logout button without styling to make things simpler.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>&lt;button onclick=\"logoutButton()\"&gt;Logout&lt;\/button&gt;<\/code><\/pre>\n\n\n\n<p>When clicked, the button calls the <span class=\"rnthl rntliteral\">logoutButton()<\/span> JavaScript function. This function makes an HTTP GET request to your ESP32\/ESP8266 on the <strong><em>\/logout<\/em><\/strong> URL. Then, in the ESP code, you should handle what happens after receiving this request.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>function logoutButton() {\n  var xhr = new XMLHttpRequest();\n  xhr.open(\"GET\", \"logout\", true);\n  xhr.send();<\/code><\/pre>\n\n\n\n<p>One second after you click the logout button, you are redirected to the logout page on the <em><strong>\/logged-out<\/strong><\/em> URL.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>  setTimeout(function(){ window.open(\"\/logged-out\",\"_self\"); }, 1000);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Handle Requests with Authentication<\/h3>\n\n\n\n<p>Every time you make a request to the ESP32 or ESP8266 to access the web server, it will check whether you&#8217;ve already entered the correct username and password to authenticate.<\/p>\n\n\n\n<p>Basically, to add authentication to your web server, you just need to add the following lines after each request:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(!request-&gt;authenticate(http_username, http_password))\n    return request-&gt;requestAuthentication();<\/code><\/pre>\n\n\n\n<p>These lines continuously pop up the authentication window until you insert the right credentials.<\/p>\n\n\n\n<p>You need to do this for all requests. This way, you ensure that you&#8217;ll only get responses if you are logged in.<\/p>\n\n\n\n<p>For example, when you try to access the root URL (ESP IP address), you add the previous two lines before sending the page. If you enter the wrong credentials, the browser will keep asking for them.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  if(!request-&gt;authenticate(http_username, http_password))\n    return request-&gt;requestAuthentication();\n  request-&gt;send_P(200, \"text\/html\", index_html, processor);\n});<\/code><\/pre>\n\n\n\n<p>Here&#8217;s another example for when the ESP receives a request on the <strong><em>\/state<\/em><\/strong> URL.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/state\", HTTP_GET, &#091;] (AsyncWebServerRequest *request) {\n  if(!request-&gt;authenticate(http_username, http_password))\n    return request-&gt;requestAuthentication();\n  request-&gt;send(200, \"text\/plain\", String(digitalRead(output)).c_str());\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Handle Logout Button<\/h3>\n\n\n\n<p>When you click the logout button, the ESP receives a request on the <strong><em>\/logout<\/em><\/strong> URL. When that happens send the response code 401.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/logout\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send(401);\n});<\/code><\/pre>\n\n\n\n<p>The response code 401 is an unauthorized error HTTP response status code indicating that the request sent by the client could not be authenticated. So, it will have the same effect as a logout &#8211; it will ask for the username and password and won&#8217;t let you access the web server again until you login.<\/p>\n\n\n\n<p>When you click the web server logout button, after one second, the ESP receives another request on the <em><strong>\/logged-out<\/strong><\/em> URL. When that happens, send the HTML text to build the logout page (<span class=\"rnthl rntliteral\">logout_html<\/span> variable).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/logged-out\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/html\", logout_html, processor);\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your ESP32 or ESP8266 board. Then, open the Serial Monitor and press the on-board RST\/EN button to get is IP address.<\/p>\n\n\n\n<p>Open a browser in your local network and type the ESP IP address.<\/p>\n\n\n\n<p>The following page should load asking for the username and password. Enter the username and password and you should get access to the web server. If you haven&#8217;t modified the code, the username is <strong>admin <\/strong>and the password is <strong>admin<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"451\" height=\"253\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Username-Password.png?resize=451%2C253&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Web Server HTTP Authentication type Username and Password\" class=\"wp-image-96378\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Username-Password.png?w=451&amp;quality=100&amp;strip=all&amp;ssl=1 451w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Username-Password.png?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 451px) 100vw, 451px\" \/><\/figure><\/div>\n\n\n<p>After typing the right username and password, you should get access to the web server.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Logout-Button.png?resize=604%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Web Server HTTP Auth Username and Password Protected\" class=\"wp-image-96379\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Logout-Button.png?w=604&amp;quality=100&amp;strip=all&amp;ssl=1 604w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Logout-Button.png?resize=300%2C209&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/figure><\/div>\n\n\n<p>You can play with the web server and see that it actually controls the ESP32 or ESP8266 on-board LED.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 board Built in LED turned on HIGH\" class=\"wp-image-96167\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>In the web server page, there&#8217;s a logout button. If you click that button, you&#8217;ll be redirected to a logout page as shown below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"256\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-Password-Protected-Web-Server-Logout.png?resize=518%2C256&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-96526\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-Password-Protected-Web-Server-Logout.png?w=518&amp;quality=100&amp;strip=all&amp;ssl=1 518w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-Password-Protected-Web-Server-Logout.png?resize=300%2C148&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 518px) 100vw, 518px\" \/><\/figure><\/div>\n\n\n<p>If you click the &#8220;return to homepage&#8221; link, you&#8217;ll be redirected to the main web server page. <\/p>\n\n\n\n<p>If you&#8217;re using Google Chrome, you&#8217;ll need to enter the username and password to access the web server again. <\/p>\n\n\n\n<p>If you&#8217;re using Firefox, you need to close all web browser tabs to completely logout. Otherwise, if you go back to the main web server page, you&#8217;ll still have access.<\/p>\n\n\n\n<p>So, we advise that you close all web browser tabs after clicking the logout button.<\/p>\n\n\n\n<p>You also need to enter the username and password if you try to get access using a different device on the local network, even though you have access on another device.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"700\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE-Demonstration.jpg?resize=440%2C700&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Web Server HTTP Authentication Username and Password Protected Arduino IDE Demonstration\" class=\"wp-image-96482\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE-Demonstration.jpg?w=440&amp;quality=100&amp;strip=all&amp;ssl=1 440w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE-Demonstration.jpg?resize=189%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 189w\" sizes=\"(max-width: 440px) 100vw, 440px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you&#8217;ve learned how to add authentication to your ESP32 and ESP8266 web servers (password protected web server). You can apply what you learned in this tutorial to any web server built with the ESPAsyncWebServer library.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. Other web server projects you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-web-server-physical-button\/\">ESP32\/ESP8266: Control Outputs with Web Server and a Physical Button Simultaneously<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-web-server-outputs-momentary-switch\/\">ESP32\/ESP8266 Web Server: Control Outputs with Momentary Switch<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-relay-web-server\/\">ESP32\/ESP8266 Relay Module Web Server using Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 and ESP8266 boards with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li>\n\n\n\n<li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects, Tutorials and Guides<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">Free ESP8266 NodeMCU Projects, Tutorials and Guides<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to add HTTP authentication with username and password to your ESP32 and ESP8266 NodeMCU web server projects using Arduino IDE. You can only access your web server if &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32\/ESP8266 Web Server HTTP Authentication (Username and Password Protected)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-web-server-http-authentication\/#more-96364\" aria-label=\"Read more about ESP32\/ESP8266 Web Server HTTP Authentication (Username and Password Protected)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":96381,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-96364","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-NodeMCU-Web-Server-HTTP-Authentication-Username-and-Password-Protected-Arduino-IDE.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/96364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=96364"}],"version-history":[{"count":2,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/96364\/revisions"}],"predecessor-version":[{"id":168079,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/96364\/revisions\/168079"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/96381"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=96364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=96364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=96364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}