-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support auto-authentication setting
- Loading branch information
Showing
13 changed files
with
557 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
examples/App/AppInitialization/Async/Callback/ReAuthenticate/ReAuthenticate.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* ABOUT: | ||
* | ||
* This is the non-blocking with callback example for how to disable the re-authentication and force the authentication. | ||
* | ||
* This example uses the DefaultNetwork class for network interface configuration. | ||
* See examples/App/NetworkInterfaces for more network examples. | ||
* | ||
* The complete usage guidelines, please read README.md or visit https://github.com/mobizt/FirebaseClient | ||
* | ||
* SYNTAX: | ||
* | ||
* 1.------------------------ | ||
* | ||
* FirebaseApp::autoRefresh(<enable>); | ||
* | ||
* <enable> - Set to true to enable auto-refresh and false for disable auto-refresh. | ||
* | ||
* 2.------------------------ | ||
* | ||
* FirebaseApp::refreshToken(); | ||
* | ||
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient | ||
*/ | ||
|
||
#include <Arduino.h> | ||
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA) | ||
#include <WiFi.h> | ||
#elif defined(ESP8266) | ||
#include <ESP8266WiFi.h> | ||
#elif __has_include(<WiFiNINA.h>) || defined(ARDUINO_NANO_RP2040_CONNECT) | ||
#include <WiFiNINA.h> | ||
#elif __has_include(<WiFi101.h>) | ||
#include <WiFi101.h> | ||
#elif __has_include(<WiFiS3.h>) || defined(ARDUINO_UNOWIFIR4) | ||
#include <WiFiS3.h> | ||
#elif __has_include(<WiFiC3.h>) || defined(ARDUINO_PORTENTA_C33) | ||
#include <WiFiC3.h> | ||
#elif __has_include(<WiFi.h>) | ||
#include <WiFi.h> | ||
#endif | ||
|
||
#include <FirebaseClient.h> | ||
|
||
#define WIFI_SSID "WIFI_AP" | ||
#define WIFI_PASSWORD "WIFI_PASSWORD" | ||
|
||
// The API key can be obtained from Firebase console > Project Overview > Project settings. | ||
#define API_KEY "Web_API_KEY" | ||
|
||
// User Email and password that already registerd or added in your project. | ||
#define USER_EMAIL "USER_EMAIL" | ||
#define USER_PASSWORD "USER_PASSWORD" | ||
|
||
void asyncCB(AsyncResult &aResult); | ||
|
||
void printResult(AsyncResult &aResult); | ||
|
||
DefaultNetwork network; | ||
|
||
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD, 300 /* expired in 300 sec */); | ||
|
||
FirebaseApp app; | ||
|
||
#if defined(ESP32) || defined(ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W) | ||
#include <WiFiClientSecure.h> | ||
WiFiClientSecure ssl_client; | ||
#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA) || defined(ARDUINO_PORTENTA_C33) || defined(ARDUINO_NANO_RP2040_CONNECT) | ||
#include <WiFiSSLClient.h> | ||
WiFiSSLClient ssl_client; | ||
#endif | ||
|
||
using AsyncClient = AsyncClientClass; | ||
|
||
AsyncClient aClient(ssl_client, getNetwork(network)); | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
|
||
Serial.print("Connecting to Wi-Fi"); | ||
while (WiFi.status() != WL_CONNECTED) | ||
{ | ||
Serial.print("."); | ||
delay(300); | ||
} | ||
Serial.println(); | ||
Serial.print("Connected with IP: "); | ||
Serial.println(WiFi.localIP()); | ||
Serial.println(); | ||
|
||
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION); | ||
|
||
Serial.println("Initializing app..."); | ||
|
||
#if defined(ESP32) || defined(ESP8266) || defined(PICO_RP2040) | ||
ssl_client.setInsecure(); | ||
#if defined(ESP8266) | ||
ssl_client.setBufferSizes(4096, 1024); | ||
#endif | ||
#endif | ||
|
||
// Initialize the FirebaseApp or auth task handler. | ||
// To deinitialize, use deinitializeApp(app). | ||
Serial.println("Initializing the app..."); | ||
initializeApp(aClient, app, getAuth(user_auth), asyncCB, "authTask"); | ||
|
||
// Disable auto authentication | ||
// From UserAuth class constructor defined above, the expire period was 300 seconds (5 min). | ||
// Then the library will not re-authenticate after 300 seconds or auth token was expired (60 minutes). | ||
// The auth token will be expired in 60 minutes. | ||
app.autoAuthenticate(false); | ||
} | ||
|
||
void loop() | ||
{ | ||
app.loop(); | ||
|
||
if (app.ready()) | ||
{ | ||
// We force the auth token to be refreshed after 60 seconds. | ||
if (app.ttl() <= 300 - 60) | ||
{ | ||
app.authenticate(); | ||
} | ||
} | ||
} | ||
|
||
void asyncCB(AsyncResult &aResult) { printResult(aResult); } | ||
|
||
void printResult(AsyncResult &aResult) | ||
{ | ||
if (aResult.isEvent()) | ||
{ | ||
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.appEvent().message().c_str(), aResult.appEvent().code()); | ||
} | ||
|
||
if (aResult.isDebug()) | ||
{ | ||
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str()); | ||
} | ||
|
||
if (aResult.isError()) | ||
{ | ||
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code()); | ||
} | ||
|
||
if (aResult.available()) | ||
{ | ||
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str()); | ||
} | ||
} |
Oops, something went wrong.