Skip to content

Commit

Permalink
Fix the infinite loop, add callback, timeout and retry limit of token…
Browse files Browse the repository at this point in the history
… generation.
  • Loading branch information
mobizt committed Mar 13, 2021
1 parent a579c64 commit f0c0526
Show file tree
Hide file tree
Showing 33 changed files with 925 additions and 453 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Arduino Client Library for ESP8266 and ESP32


Google's Firebase Arduino Client Library for ESP8266 and ESP32 v 2.0.5
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v 2.0.6


This library supports ESP8266 and ESP32 MCU from Espressif. The following are platforms in which the libraries are also available (RTDB only).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ FirebaseAuth auth;
/* 5. Define the FirebaseConfig data for config data */
FirebaseConfig config;

/* The calback function to print the token generation status */
void tokenStatusCallback(TokenInfo info);

/* The function to print the operating results */
void printResult(FirebaseData &data);

Expand Down Expand Up @@ -93,6 +96,12 @@ void setup()

Firebase.reconnectWiFi(true);

/** Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback;

/** Assign the maximum retry of token generation */
config.max_token_generation_retry = 5;

/* Now we start to signin using access token */

/** Initialize the library with the Firebase authen and config.
Expand All @@ -116,44 +125,58 @@ void loop()
dataMillis = millis();

/* Get the token status */
struct token_info_t info = Firebase.authTokenInfo();
Serial.println("------------------------------------");
if (info.status == token_status_error)
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
Serial.printf("Token error: %s\n\n", getTokenError(info).c_str());
}
else
TokenInfo info = Firebase.authTokenInfo();
if (info.status == token_status_ready)
{
Serial.printf("Token info: type = %s, status = %s\n\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
}

Serial.println("------------------------------------");
Serial.println("Set int test...");
Serial.println("------------------------------------");
Serial.println("Set int test...");

String Path = path + "/int";
String Path = path + "/int";

if (Firebase.RTDB.set(&fbdo, Path.c_str(), count++))
{
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
Serial.println("ETag: " + fbdo.ETag());
Serial.print("VALUE: ");
printResult(fbdo);
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("------------------------------------");
Serial.println();
if (Firebase.RTDB.set(&fbdo, Path.c_str(), count++))
{
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
Serial.println("ETag: " + fbdo.ETag());
Serial.print("VALUE: ");
printResult(fbdo);
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}
}
}

void tokenStatusCallback(TokenInfo info)
{
/** fb_esp_auth_token_status enum
* token_status_uninitialized,
* token_status_on_initialize,
* token_status_on_signing,
* token_status_on_request,
* token_status_on_refresh,
* token_status_ready,
* token_status_error
*/
if (info.status == token_status_error)
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
Serial.printf("Token error: %s\n", getTokenError(info).c_str());
}
else
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
}
}

void printResult(FirebaseData &data)
{

Expand Down Expand Up @@ -318,6 +341,9 @@ String getTokenStatus(struct token_info_t info)
case token_status_uninitialized:
return "uninitialized";

case token_status_on_initialize:
return "on initializing";

case token_status_on_signing:
return "on signing";

Expand Down
90 changes: 59 additions & 31 deletions examples/Authentications/Custom_Token/Custom_Token.ino
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ FirebaseAuth auth;
/* 7. Define the FirebaseConfig data for config data */
FirebaseConfig config;

/* The calback function to print the token generation status */
void tokenStatusCallback(TokenInfo info);

/* The helper function to modify the database rules (optional) */
void prepareDatabaseRules(const char *path, const char *var, const char *readVal, const char *writeVal);

Expand Down Expand Up @@ -201,6 +204,12 @@ void setup()
String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
prepareDatabaseRules(base_path.c_str(), var.c_str(), val.c_str(), val.c_str());

/** Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback;

/** Assign the maximum retry of token generation */
config.max_token_generation_retry = 5;

/* Now we start to signin using custom token */

/** Initialize the library with the Firebase authen and config.
Expand Down Expand Up @@ -236,41 +245,57 @@ void loop()
dataMillis = millis();

/* Get the token status */
struct token_info_t info = Firebase.authTokenInfo();
Serial.println("------------------------------------");
if (info.status == token_status_error)
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
Serial.printf("Token error: %s\n\n", getTokenError(info).c_str());
}
else
TokenInfo info = Firebase.authTokenInfo();
if (info.status == token_status_ready)
{
Serial.printf("Token info: type = %s, status = %s\n\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
}

Serial.println("------------------------------------");
Serial.println("Set int test...");
Serial.println("------------------------------------");
Serial.println("Set int test...");

String Path = path + "/int";
String Path = path + "/int";

if (Firebase.RTDB.set(&fbdo, Path.c_str(), count++))
{
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
Serial.println("ETag: " + fbdo.ETag());
Serial.print("VALUE: ");
printResult(fbdo);
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("------------------------------------");
Serial.println();
if (Firebase.RTDB.set(&fbdo, Path.c_str(), count++))
{
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
Serial.println("ETag: " + fbdo.ETag());
Serial.print("VALUE: ");
printResult(fbdo);
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}


}
}

void tokenStatusCallback(TokenInfo info)
{
/** fb_esp_auth_token_status enum
* token_status_uninitialized,
* token_status_on_initialize,
* token_status_on_signing,
* token_status_on_request,
* token_status_on_refresh,
* token_status_ready,
* token_status_error
*/
if (info.status == token_status_error)
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
Serial.printf("Token error: %s\n", getTokenError(info).c_str());
}
else
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
}
}

Expand Down Expand Up @@ -510,6 +535,9 @@ String getTokenStatus(struct token_info_t info)
case token_status_uninitialized:
return "uninitialized";

case token_status_on_initialize:
return "on initializing";

case token_status_on_signing:
return "on signing";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ FirebaseAuth auth;
/* 7. Define the FirebaseConfig data for config data */
FirebaseConfig config;

/* The calback function to print the token generation status */
void tokenStatusCallback(TokenInfo info);

/* The helper function to modify the database rules (optional) */
void prepareDatabaseRules(const char *path, const char *var, const char *readVal, const char *writeVal);

Expand Down Expand Up @@ -167,6 +170,12 @@ void setup()
String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
prepareDatabaseRules(base_path.c_str(), var.c_str(), val.c_str(), val.c_str());

/** Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback;

/** Assign the maximum retry of token generation */
config.max_token_generation_retry = 5;

/* Now we start to signin using custom token */

/** Initialize the library with the Firebase authen and config.
Expand Down Expand Up @@ -202,44 +211,58 @@ void loop()
dataMillis = millis();

/* Get the token status */
struct token_info_t info = Firebase.authTokenInfo();
Serial.println("------------------------------------");
if (info.status == token_status_error)
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
Serial.printf("Token error: %s\n\n", getTokenError(info).c_str());
}
else
TokenInfo info = Firebase.authTokenInfo();
if (info.status == token_status_ready)
{
Serial.printf("Token info: type = %s, status = %s\n\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
}

Serial.println("------------------------------------");
Serial.println("Set int test...");
Serial.println("------------------------------------");
Serial.println("Set int test...");

String Path = path + "/int";
String Path = path + "/int";

if (Firebase.RTDB.set(&fbdo, Path.c_str(), count++))
{
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
Serial.println("ETag: " + fbdo.ETag());
Serial.print("VALUE: ");
printResult(fbdo);
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("------------------------------------");
Serial.println();
if (Firebase.RTDB.set(&fbdo, Path.c_str(), count++))
{
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
Serial.println("ETag: " + fbdo.ETag());
Serial.print("VALUE: ");
printResult(fbdo);
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}
}
}

void tokenStatusCallback(TokenInfo info)
{
/** fb_esp_auth_token_status enum
* token_status_uninitialized,
* token_status_on_initialize,
* token_status_on_signing,
* token_status_on_request,
* token_status_on_refresh,
* token_status_ready,
* token_status_error
*/
if (info.status == token_status_error)
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
Serial.printf("Token error: %s\n", getTokenError(info).c_str());
}
else
{
Serial.printf("Token info: type = %s, status = %s\n", getTokenType(info).c_str(), getTokenStatus(info).c_str());
}
}

/* The helper function to modify the database rules (optional) */
void prepareDatabaseRules(const char *path, const char *var, const char *readVal, const char *writeVal)
{
Expand Down Expand Up @@ -476,6 +499,9 @@ String getTokenStatus(struct token_info_t info)
case token_status_uninitialized:
return "uninitialized";

case token_status_on_initialize:
return "on initializing";

case token_status_on_signing:
return "on signing";

Expand Down
Loading

0 comments on commit f0c0526

Please sign in to comment.