{"id":107356,"date":"2025-04-30T08:24:33","date_gmt":"2025-04-30T08:24:33","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=107356"},"modified":"2025-05-12T14:32:23","modified_gmt":"2025-05-12T14:32:23","slug":"esp32-esp8266-firebase-authentication","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-authentication\/","title":{"rendered":"ESP32\/ESP8266: Firebase Authentication (Email and Password)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to authenticate to Firebase using your ESP32 or ESP8266 board with an email and password, and get the user UID. This is useful for restricting or allowing access to certain users or creating multiple users that can access the same Firebase project. Additionally, this is helpful when setting up database rules to protect your project&#8217;s data.<\/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\/2021\/11\/ESP32-ESP8266-Firebase-Authentication.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Firebase Authentication Email Password\" class=\"wp-image-169564\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/ESP32-ESP8266-Firebase-Authentication.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/ESP32-ESP8266-Firebase-Authentication.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/ESP32-ESP8266-Firebase-Authentication.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/ESP32-ESP8266-Firebase-Authentication.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/ESP32-ESP8266-Firebase-Authentication.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\"><em>Updated 30 April 2025.<\/em><\/p>\n\n\n\n<p>Other Firebase Tutorials with the ESP32 that you might be interested in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-realtime-database\/\">ESP32: Getting Started with Firebase (Realtime Database)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-web-app\/\">ESP32 with Firebase \u2013 Creating a Web App<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is Firebase?<\/strong><\/h2>\n\n\n\n<p>Firebase is Google&#8217;s mobile application development platform that helps you build, improve, and grow your app. It has many services used to manage data from any android, IOS, or web application like authentication, <a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-realtime-database\/\">realtime database<\/a>, <a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-web-app\/\">hosting<\/a>, etc. In this tutorial, we&#8217;ll focus on the authentication part.<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"196\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Firebase-Logo.png?resize=750%2C196&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Logo\" class=\"wp-image-169096\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Firebase-Logo.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Firebase-Logo.png?resize=300%2C78&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#create-firebase-project\">Create Firebase Project<\/a><\/li>\n\n\n\n<li><a href=\"#Set-Authentication-Methods\">Set Authentication Methods<\/a><\/li>\n\n\n\n<li><a href=\"#firebase-project-api-key\">Get Project API Key<\/a><\/li>\n\n\n\n<li><a href=\"#Firebase-Authentication-ESP32-ESP8266\">Authentication with ESP32\/ESP8266<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-firebase-project\">1) <span style=\"font-size: inherit;\">Create Firebase Project<\/span><\/h2>\n\n\n\n<p>Follow the next instructions to create a new project on Firebase.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to <a href=\"https:\/\/firebase.google.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Firebase <\/a>and sign in using a Google Account.<\/li>\n\n\n\n<li>Go to the <a href=\"https:\/\/console.firebase.google.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Firebase Console<\/a> and create a new project.<\/li>\n\n\n\n<li>Give a name to your project, for example: <em>ESP-Project,<\/em> and click <strong>Continue<\/strong>.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105729\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/1-create-firebase-project.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Set Up Firebase Project for ESP32 and ESP8266 Step 1\"><\/figure><\/li>\n\n\n\n<li>Next, enable or disable AI assistance for your project. This is optional.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105729\" style=\"width: 739px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/2-create-firebase-project-ai-assistance.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Set Up Firebase Project for ESP32 and ESP8266 - Enable AI assistant\"><\/figure><\/li>\n\n\n\n<li>Disable the option <em>Enable Google Analytics<\/em> for this project, as it is not needed. Then, click <strong>Create project<\/strong>.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105730\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/3-create-firebase-project.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Disable Google Analytics for firebase project\"><\/figure><\/li>\n\n\n\n<li>It will take a few seconds to set up your project. Click <strong>Continue <\/strong>when it&#8217;s ready.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105731\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/5-Firebase-project-ready.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Project for ESP32 Ready\"><\/figure><br><\/li>\n\n\n\n<li>You&#8217;ll be redirected to your Project console page.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105732\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/6-Firebase-console-ESP-project.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase console project\"><\/figure><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Set-Authentication-Methods\">2) Set Authentication Methods<\/h2>\n\n\n\n<p>To allow authentication with email and password, first, you need to set authentication methods for your app. <\/p>\n\n\n\n<p>&#8220;Most apps need to know the identity of a user. In other words, it takes care of logging in and identifying the users (in this case, the ESP32 or ESP8266). Knowing a user&#8217;s identity allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user&#8217;s devices.&#8221; To learn more about the authentication methods, you can <a href=\"https:\/\/firebase.google.com\/docs\/auth\" target=\"_blank\" rel=\"noreferrer noopener\">read the documentation<\/a>.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>On the left sidebar, click on <strong>Build <\/strong>&gt; <strong>Authentication <\/strong>and then on <strong>Get started<\/strong>. <br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105735\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/7-set-authentication.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase project set authentication\"><\/figure><\/li>\n\n\n\n<li>There are several authentication methods like email and password, Google Account, Facebook account, and others.<figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105740\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/8-Firebase-authentication-methods.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"SFirebase authentication methods\"><\/figure><\/li>\n\n\n\n<li>Select <strong>Email\/Password<\/strong> and <strong>enable <\/strong>that authentication method. Then, click <strong>Save<\/strong>.<figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105741\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/9-enable-email-password-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Enable Email Password Sign in Firebase\"><\/figure><\/li>\n\n\n\n<li>Then, at the top, click on the <strong>Users <\/strong>tab. Then, click on <strong>Add user<\/strong>. <figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105741\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/10-add-user-email-password-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Create a new user\"><\/figure><\/li>\n\n\n\n<li>Create a new user with an email and password. The email can be your personal email. Create a password for that user (you need to remember the password later). Finally, click on <strong>Add user<\/strong>.<figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105741\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/11-add-user-email-pass.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase add user email and password\"><\/figure><\/li>\n\n\n\n<li>The User will show up on the list of users. You can see information about the user, like when it was created, the last time it signed in, and its user UID.<figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105741\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/user-created-in-firebase-esp32.jpg?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase User Created\"><\/figure><\/li>\n<\/ol>\n\n\n\n<p>Firebase creates a unique UID for each registered user. The user UID allows us to identify the user and keep track of the user to provide or deny access to the project or the database. There&#8217;s also a column that registers the date of the last sign-in. At the moment, it is empty because we haven&#8217;t signed in with that user yet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"firebase-project-api-key\">3) Get Project API Key<\/h2>\n\n\n\n<p>To interface with your Firebase project using the ESP32 or ESP8266 boards, you need to get your project API key. Follow the next steps to get your project API key.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>To get your project&#8217;s API key, on the left sidebar click on <strong>Project Settings<\/strong>.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105748\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/17-project-settings-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Realtime Database Project Settings\"><\/figure><\/li>\n\n\n\n<li>Copy the API Key to a safe place because you&#8217;ll need it later.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105749\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/18-project-api-key-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Project API Key\"><\/figure><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Firebase-Authentication-ESP32-ESP8266\">4) <span style=\"font-size: inherit;\">Authentication with ESP32<\/span>\/ESP8266 <\/h2>\n\n\n\n<p>Now that your Firebase Project is created and you&#8217;ve set up the authentication method, you&#8217;ll learn to log in with the ESP32 or ESP8266 using the authorized user email and password.<\/p>\n\n\n\n<p>To program the boards, you can use <a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Arduino IDE<\/a>, <a href=\"https:\/\/randomnerdtutorials.com\/vs-code-platformio-ide-esp32-esp8266-arduino\/\">VS Code with the PlatformIO or pioarduino extensions<\/a>, or other suitable software.<\/p>\n\n\n\n<p><strong>Note: <\/strong> for firebase projects, we recommend using VS Code with the PlatformIO or pioarduino extension because if you want to develop a web application to make the bridge between the ESP and Firebase, VS Code provides all the tools to do that. However, we won&#8217;t build the web application in this tutorial, so you can use Arduino IDE.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the FirebaseClient Library<\/h3>\n\n\n\n<p>To use Firebase with the ESP32 and ESP8266, we&#8217;ll use the <a href=\"https:\/\/github.com\/mobizt\/FirebaseClient\" target=\"_blank\" rel=\"noopener\" title=\"\">FirebaseClient library<\/a>. This library is compatible with the ESP32, ESP8266, and many other boards.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Installation &#8211; VS Code + PlatformIO<\/h4>\n\n\n\n<p>If you&#8217;re using VS Code with the PlatformIO extension, click on the <strong>PIO Home<\/strong> icon and then select the <strong>Libraries tab<\/strong>. Search for &#8220;<strong>FirebaseClient<\/strong>&#8220;. Select the <strong>Firebase Client Library<\/strong> by Mobitz.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"646\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Install-FirebsaeClient-Library-VS-Code.jpg?resize=750%2C646&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install FirebaseClient Library VS Code\" class=\"wp-image-169174\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Install-FirebsaeClient-Library-VS-Code.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Install-FirebsaeClient-Library-VS-Code.jpg?resize=300%2C258&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Then, click <strong>Add to Project<\/strong> and select the project you&#8217;re working on.<\/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=\"750\" height=\"341\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Add-FirebaseClient-library-to-project-VS-Code.jpg?resize=750%2C341&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Add FirebaseClient Library ro project in VS Code\" class=\"wp-image-169175\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Add-FirebaseClient-library-to-project-VS-Code.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Add-FirebaseClient-library-to-project-VS-Code.jpg?resize=300%2C136&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Also, change the monitor speed to 115200 by adding the following line to the <span class=\"rnthl rntliteral\">platformio.ini<\/span> file of your project:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>monitor_speed = 115200<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Installation &#8211; Arduino IDE<\/h4>\n\n\n\n<p>If you&#8217;re using Arduino IDE, follow the next steps to install the library.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong><\/li>\n\n\n\n<li>Search for <strong>FirebaseClient<\/strong> and install the <strong>FirebaseClient<\/strong> by Mobitz. We&#8217;re using version 2.0.3.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"790\" height=\"586\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/install-Firebase-client-arduino-ide.png?resize=790%2C586&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install Firebase Client Library Arduino IDE\" class=\"wp-image-169118\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/install-Firebase-client-arduino-ide.png?w=790&amp;quality=100&amp;strip=all&amp;ssl=1 790w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/install-Firebase-client-arduino-ide.png?resize=300%2C223&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/install-Firebase-client-arduino-ide.png?resize=768%2C570&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 790px) 100vw, 790px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span style=\"font-size: inherit;\">ESP32<\/span>\/ESP8266 Firebase Authentication Email\/Password (Get User UID)<\/h3>\n\n\n\n<p>Copy the following code to the Arduino IDE or to the <span class=\"rnthl rntliteral\">main.cpp<\/span> file if you&#8217;re using VS Code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\r\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n  Complete project details at our blog: https:\/\/RandomNerdTutorials.com\/esp32-esp8266-firebase-authentication\/\r\n  Based on this example: https:\/\/github.com\/mobizt\/FirebaseClient\/blob\/main\/examples\/App\/AppInitialization\/UserAuth\/UserAuth.ino\r\n*\/\r\n\r\n#include &lt;Arduino.h&gt;\r\n#if defined(ESP32)\r\n  include &lt;WiFi.h&gt;\r\n#elif defined(ESP8266)\r\n  #include &lt;ESP8266WiFi.h&gt;\r\n#endif\r\n#include &lt;WiFiClientSecure.h&gt;\r\n#include &lt;FirebaseClient.h&gt;\r\n#include &quot;ExampleFunctions.h&quot; \/\/ Provides the functions used in the examples.\r\n\r\n\/\/ Network and Firebase credentials\r\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\r\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\r\n\r\n#define Web_API_KEY &quot;REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY&quot;\r\n\r\n#define USER_EMAIL &quot;REPLACE_WITH_FIREBASE_PROJECT_EMAIL_USER&quot;\r\n#define USER_PASS &quot;REPLACE_WITH_FIREBASE_PROJECT_USER_PASS&quot;\r\n\r\n\/\/ User functions\r\nvoid processData(AsyncResult &amp;aResult);\r\n\r\n\/\/ Authentication\r\nUserAuth user_auth(Web_API_KEY, USER_EMAIL, USER_PASS);\r\n\r\n\/\/ Firebase components\r\nFirebaseApp app;\r\nWiFiClientSecure ssl_client;\r\nusing AsyncClient = AsyncClientClass;\r\nAsyncClient aClient(ssl_client);\r\nAsyncResult dbResult;\r\n\r\nbool taskComplete = false;\r\n\r\nvoid setup(){\r\n  Serial.begin(115200);\r\n\r\n  \/\/ Connect to Wi-Fi\r\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\r\n  Serial.print(&quot;Connecting to Wi-Fi&quot;);\r\n  while (WiFi.status() != WL_CONNECTED)    {\r\n    Serial.print(&quot;.&quot;);\r\n    delay(300);\r\n  }\r\n  Serial.println();\r\n  Serial.print(&quot;Connected with IP: &quot;);\r\n  Serial.println(WiFi.localIP());\r\n  Serial.println();\r\n\r\n  \/\/ Configure SSL client\r\n  ssl_client.setInsecure();\r\n  #if defined(ESP32)\r\n    ssl_client.setConnectionTimeout(1000);\r\n    ssl_client.setHandshakeTimeout(5);\r\n  #elif defined(ESP8266)\r\n    ssl_client.setTimeout(1000); \/\/ Set connection timeout\r\n    ssl_client.setBufferSizes(4096, 1024); \/\/ Set buffer sizes\r\n  #endif\r\n\r\n  \/\/ Initialize Firebase\r\n  initializeApp(aClient, app, getAuth(user_auth), processData, &quot;\ud83d\udd10 authTask&quot;);\r\n}\r\n\r\nvoid loop(){\r\n  \/\/ Maintain authentication and async tasks\r\n  app.loop();\r\n\r\n  \/\/ Check if authentication is ready\r\n  if (app.ready() &amp;&amp; !taskComplete){\r\n    taskComplete = true;\r\n    \/\/ Print authentication info\r\n    Serial.println(&quot;Authentication Information&quot;);\r\n    Firebase.printf(&quot;User UID: %s\\n&quot;, app.getUid().c_str());\r\n    Firebase.printf(&quot;Auth Token: %s\\n&quot;, app.getToken().c_str());\r\n    Firebase.printf(&quot;Refresh Token: %s\\n&quot;, app.getRefreshToken().c_str());\r\n    print_token_type(app);\r\n  }\r\n}\r\n\r\nvoid processData(AsyncResult &amp;aResult){\r\n  if (!aResult.isResult())\r\n    return;\r\n\r\n  if (aResult.isEvent())\r\n    Firebase.printf(&quot;Event task: %s, msg: %s, code: %d\\n&quot;, aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().code());\r\n\r\n  if (aResult.isDebug())\r\n    Firebase.printf(&quot;Debug task: %s, msg: %s\\n&quot;, aResult.uid().c_str(), aResult.debug().c_str());\r\n\r\n  if (aResult.isError())\r\n    Firebase.printf(&quot;Error task: %s, msg: %s, code: %d\\n&quot;, aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());\r\n\r\n  if (aResult.available())\r\n    Firebase.printf(&quot;task: %s, payload: %s\\n&quot;, aResult.uid().c_str(), aResult.c_str());\r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Firebase-ESP\/raw\/main\/ESP-Firebase-Authentication\/ESP-Firebase-Authentication.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You need to insert your network credentials, URL database, and project API key for the project to work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Continue reading to learn how the code works, or skip to the <a href=\"#demonstration\">demonstration section<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Include Libraries<\/h4>\n\n\n\n<p>First, include the required libraries. The&nbsp;<span class=\"rnthl rntliteral\">WiFi.h<\/span>&nbsp;library to connect the ESP32 to the internet (or the&nbsp;<span class=\"rnthl rntliteral\">ESP8266WiFi.h<\/span>&nbsp;library for the ESP8266 board), the&nbsp;<span class=\"rnthl rntliteral\">FirebaseClient.h<\/span>&nbsp;and <span class=\"rnthl rntliteral\">WiFiClientSecure<\/span> libraries to interface the boards with Firebase. We also include the <span class=\"rnthl rntliteral\">ExampleFunctions.h<\/span> file which is included in the library that comes with some additional functions for this example.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Arduino.h&gt;\n#if defined(ESP32)\n    #include &lt;WiFi.h&gt;\n#elif defined(ESP8266)\n    #include &lt;ESP8266WiFi.h&gt;\n#endif\n#include &lt;WiFiClientSecure.h&gt;\n#include &lt;FirebaseClient.h&gt;\n#include \"ExampleFunctions.h\" \/\/ Provides the functions used in the examples.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Defining Credentials<\/h4>\n\n\n\n<p>Include your network credentials in the following lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define WIFI_SSID \"REPLACE_WITH_YOUR_SSID\"\n#define WIFI_PASSWORD \"REPLACE_WITH_YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Insert your Firebase project API key.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define Web_API_KEY \"REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY\"<\/code><\/pre>\n\n\n\n<p>Set the Firebase email user and corresponding password.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define USER_EMAIL \"REPLACE_WITH_FIREBASE_PROJECT_EMAIL_USER\"\n#define USER_PASS \"REPLACE_WITH_FIREBASE_PROJECT_USER_PASS\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Declaring Firebase Authentication and Components<\/h4>\n\n\n\n<p>The following line creates an authentication object using the project API key, the project user email, and password.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>UserAuth user_auth(Web_API_KEY, USER_EMAIL, USER_PASS);<\/code><\/pre>\n\n\n\n<p>This creates a <span class=\"rnthl rntliteral\">FirebaseApp<\/span> instance called <span class=\"rnthl rntliteral\">app<\/span> that refers to the Firebase application.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>FirebaseApp app;<\/code><\/pre>\n\n\n\n<p>The following lines set up the asynchronous communication framework for interacting with Firebase&#8217;s Realtime Database. Basically, you create an SSL client using the <span class=\"rnthl rntliteral\">WiFiClientSecure<\/span> library. Then, you instantiate an Asynchronous client called <span class=\"rnthl rntliteral\">aClient<\/span> that enables secure HTTPS. This will allow you to handle network operations asynchronously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFiClientSecure ssl_client;\nusing AsyncClient = AsyncClientClass;\nAsyncClient aClient(ssl_client);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor and connect the board to your Wi-Fi network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void setup(){\n  Serial.begin(115200);\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n  Serial.print(\"Connecting to Wi-Fi\");\n  while (WiFi.status() != WL_CONNECTED)    {\n    Serial.print(\".\");\n    delay(300);\n  }\n  Serial.println();<\/code><\/pre>\n\n\n\n<p>Configure the SSL Client (different for ESP32 and ESP8266).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Configure SSL client\nssl_client.setInsecure();\n#if defined(ESP32)\n  ssl_client.setConnectionTimeout(1000);\n  ssl_client.setHandshakeTimeout(5);\n#elif defined(ESP8266)\n  ssl_client.setTimeout(1000); \/\/ Set connection timeout\n  ssl_client.setBufferSizes(4096, 1024); \/\/ Set buffer sizes\n#endif<\/code><\/pre>\n\n\n\n<p>The following line initializes the Firebase app with authentication and sets the <span class=\"rnthl rntliteral\">processData()<\/span> as the callback function for async results (this means that any results from the <span class=\"rnthl rntliteral\">initializeApp()<\/span> function will be handled on the <span class=\"rnthl rntliteral\">processData()<\/span> callback function).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>initializeApp(aClient, app, getAuth(user_auth), processData, \"\ud83d\udd10 authTask\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>The Firebase library we&#8217;re using works asynchronously and with callback functions. This means that when an event happens, the corresponding assigned callback functions will run. To keep the Firebase app running, handling authentication and asynchronous tasks, we need to add <span class=\"rnthl rntliteral\">app.loop()<\/span> at the start of our <span class=\"rnthl rntliteral\">loop()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop(){\n  app.loop();<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">app.ready()<\/span> command checks if Firebase authentication is complete and ready, so that we can proceed with other Firebase operations (like getting the User UID). We also have a control variable <span class=\"rnthl rntliteral\">taskComplete<\/span> so that is just runs once to print the user UID.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (app.ready() &amp;&amp; !taskComplete){\n  taskComplete = true;<\/code><\/pre>\n\n\n\n<p>Then, we can get the user UID with <span class=\"rnthl rntliteral\">app.getUid()<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Firebase.printf(\"User UID: %sn\", app.getUid().c_str());<\/code><\/pre>\n\n\n\n<p>We also print more information about the app auth token.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code> Firebase.printf(\"Auth Token: %sn\", app.getToken().c_str());\n Firebase.printf(\"Refresh Token: %sn\", app.getRefreshToken().c_str());\n print_token_type(app);<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h1>\n\n\n\n<p>After uploading the code, open the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<p><strong>Note<\/strong>: if you&#8217;re using VS Code, you need to add the following line to your <span class=\"rnthl rntliteral\">platformio.ini<\/span> file to change the baud rate. Then, save your file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>monitor_speed = 115200<\/code><\/pre>\n\n\n\n<p>Reset your board by pressing the on-board EN\/RST button.<\/p>\n\n\n\n<p>The ESP32\/ESP8266 authenticates successfully and prints information about the user UID and the authentication token.<\/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=\"699\" height=\"371\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/Authentication_Get_User_UID.png?resize=699%2C371&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32\/ESP8266 Get User UID Firebase\" class=\"wp-image-169543\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/Authentication_Get_User_UID.png?w=699&amp;quality=100&amp;strip=all&amp;ssl=1 699w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/Authentication_Get_User_UID.png?resize=300%2C159&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 699px) 100vw, 699px\" \/><\/figure><\/div>\n\n\n<p>Then, go to your Firebase project console to check if it signed in as a user. Go to <strong>Authentication<\/strong> &gt; <strong>Users<\/strong>. You should have the current date in the <strong>Signed<\/strong> <strong>In<\/strong> field. Additionally, you&#8217;ll see that the User UID matches the UID printed by your ESP board in the Serial Monitor.<\/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=\"665\" height=\"236\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/user-logged-in-firebase-esp32.png?resize=665%2C236&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Users Last Sign-in\" class=\"wp-image-169547\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/user-logged-in-firebase-esp32.png?w=665&amp;quality=100&amp;strip=all&amp;ssl=1 665w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/user-logged-in-firebase-esp32.png?resize=300%2C106&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 665px) 100vw, 665px\" \/><\/figure><\/div>\n\n\n<p>Congratulations! You successfully signed in your ESP32\/ESP8266 board as a user. You can combine this example with one of our previous tutorials to send sensor readings to the database as an authorized user: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-realtime-database\/\">ESP32: Getting Started with Firebase (Realtime Database)<\/a> or you can follow this project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-web-app-sensor\/\">ESP32\/ESP8266: Firebase Web App to Display Sensor Readings (with Authentication)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to set up the ESP32\/ESP8266 board as a user that can access your Firebase project with email and password. This was just a simple example that you can apply to more advanced projects. <\/p>\n\n\n\n<p>Setting up the ESP as a user allows you to identify the user to enable or restrict access to the Firebase Project or to the database or to specific database nodes\u2014allows you to set up database rules.<\/p>\n\n\n\n<p>We have a project that shows how to use this feature to create database rules and to build a web app with login\/logout features to authorize and restrict access to the data. You can check it out on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-web-app-sensor\/\">ESP32\/ESP8266: Firebase Web App to Display Sensor Readings (with Authentication)<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you want to learn more about building a fully-featured Firebase Web App to control and monitor your ESP32 and ESP8266 boards from anywhere, take a look at our eBook:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\"><strong>Firebase Web App with ESP32 and ESP8266<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<p>Other resources that you might find helpful:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with the ESP32\/ESP8266<\/a><\/li>\n\n\n\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<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to authenticate to Firebase using your ESP32 or ESP8266 board with an email and password, and get the user UID. This is useful for &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32\/ESP8266: Firebase Authentication (Email and Password)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-authentication\/#more-107356\" aria-label=\"Read more about ESP32\/ESP8266: Firebase Authentication (Email and Password)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":169564,"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-107356","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\/2021\/11\/ESP32-ESP8266-Firebase-Authentication.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/107356","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=107356"}],"version-history":[{"count":23,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/107356\/revisions"}],"predecessor-version":[{"id":170039,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/107356\/revisions\/170039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/169564"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=107356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=107356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=107356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}