Advertisement
safwan092

Untitled

Oct 14th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1.  
  2. /**
  3. Created by K. Suwatchai (Mobizt)
  4.  
  5. Email: k_suwatchai@hotmail.com
  6.  
  7. Github: https://github.com/mobizt/Firebase-ESP8266
  8.  
  9. Copyright (c) 2022 mobizt
  10.  
  11. */
  12.  
  13. /** This example will show how to authenticate as a user with Email and password.
  14.  
  15. You need to enable Email/Password provider.
  16. In Firebase console, select Authentication, select Sign-in method tab,
  17. under the Sign-in providers list, enable Email/Password provider.
  18.  
  19. From this example, the user will be granted to access the specific location that matches
  20. the user uid.
  21.  
  22. This example will modify the database rules to set up the security rule which need to
  23. guard the unauthorized access with the user Email.
  24. */
  25.  
  26. #if defined(ESP32)
  27. #include <WiFi.h>
  28. #include <FirebaseESP32.h>
  29. #elif defined(ESP8266)
  30. #include <ESP8266WiFi.h>
  31. #include <FirebaseESP8266.h>
  32. #endif
  33.  
  34. // Provide the token generation process info.
  35. #include <addons/TokenHelper.h>
  36.  
  37. // Provide the RTDB payload printing info and other helper functions.
  38. #include <addons/RTDBHelper.h>
  39.  
  40. /* 1. Define the WiFi credentials */
  41. #define WIFI_SSID "network"
  42. #define WIFI_PASSWORD "123456789"
  43.  
  44. /** 2. Define the API key
  45.  
  46. The API key (required) can be obtained since you created the project and set up
  47. the Authentication in Firebase console. Then you will get the API key from
  48. Firebase project Web API key in Project settings, on General tab should show the
  49. Web API Key.
  50.  
  51. You may need to enable the Identity provider at https://console.cloud.google.com/customer-identity/providers
  52. Select your project, click at ENABLE IDENTITY PLATFORM button.
  53. The API key also available by click at the link APPLICATION SETUP DETAILS.
  54.  
  55. */
  56. #define API_KEY "AIzaSyCcrTq6x7TGHXQL4qQ3E48zDsqkaXBJQtw"
  57.  
  58. /* 3. Define the user Email and password that already registerd or added in your project */
  59. #define USER_EMAIL "project12338@gmail.com"
  60. #define USER_PASSWORD "123456789"
  61.  
  62. /* 4. If work with RTDB, define the RTDB URL */
  63. #define DATABASE_URL "kneetracking-default-rtdb.europe-west1.firebasedatabase.app/" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
  64.  
  65. /** 5. Define the database secret (optional)
  66.  
  67. This database secret needed only for this example to modify the database rules
  68.  
  69. If you edit the database rules yourself, this is not required.
  70. */
  71. #define DATABASE_SECRET "AaATdr2hwU6v5KUAITOHyG7KGl3BfhdQMEC2XFUQ"
  72.  
  73. /* 6. Define the Firebase Data object */
  74. FirebaseData fbdo;
  75.  
  76. /* 7. Define the FirebaseAuth data for authentication data */
  77. FirebaseAuth auth;
  78.  
  79. /* 8. Define the FirebaseConfig data for config data */
  80. FirebaseConfig config;
  81.  
  82. unsigned long dataMillis = 0;
  83.  
  84. void setup()
  85. {
  86.  
  87. Serial.begin(115200);
  88.  
  89. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  90. Serial.print("Connecting to Wi-Fi");
  91. while (WiFi.status() != WL_CONNECTED)
  92. {
  93. Serial.print(".");
  94. delay(300);
  95. }
  96. Serial.println();
  97. Serial.print("Connected with IP: ");
  98. Serial.println(WiFi.localIP());
  99. Serial.println();
  100.  
  101. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  102.  
  103. /* Assign the api key (required) */
  104. config.api_key = API_KEY;
  105.  
  106. /* Assign the user sign in credentials */
  107. auth.user.email = USER_EMAIL;
  108. auth.user.password = USER_PASSWORD;
  109.  
  110. /* Assign the RTDB URL */
  111. config.database_url = DATABASE_URL;
  112.  
  113. Firebase.reconnectWiFi(true);
  114. fbdo.setResponseSize(4096);
  115.  
  116. String base_path = "/UsersData/";
  117.  
  118. /* Assign the callback function for the long running token generation task */
  119. config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  120.  
  121. /** Assign the maximum retry of token generation */
  122. config.max_token_generation_retry = 5;
  123.  
  124. /* Initialize the library with the Firebase authen and config */
  125. Firebase.begin(&config, &auth);
  126.  
  127. /** Now modify the database rules (if not yet modified)
  128.  
  129. The user, <user uid> in this case will be granted to read and write
  130. at the certain location i.e. "/UsersData/<user uid>".
  131.  
  132. If you database rules has been modified, please comment this code out.
  133.  
  134. The character $ is to make a wildcard variable (can be any name) represents any node key
  135. which located at some level in the rule structure and use as reference variable
  136. in .read, .write and .validate rules
  137.  
  138. For this case $userId represents any <user uid> node that places under UsersData node i.e.
  139. /UsersData/<user uid> which <user uid> is user UID.
  140.  
  141. Please check your the database rules to see the changes after run the below code.
  142. */
  143. String var = "$userId";
  144. String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
  145. Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
  146.  
  147. /** path for user data is now "/UsersData/<user uid>"
  148. The user UID can be taken from auth.token.uid
  149. */
  150. }
  151.  
  152. void loop()
  153. {
  154.  
  155. if (Firebase.ready() && (millis() - dataMillis > 1000))
  156. {
  157. dataMillis = millis();
  158. Firebase.setIntAsync(fbdo, "/test/int", count);
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement