mobizt / Firebase-ESP32

[DEPRECATED]🔥 Firebase RTDB Arduino Library for ESP32. The complete, fast, secured and reliable Firebase Arduino client library that supports CRUD (create, read, update, delete) and Stream operations.
MIT License
414 stars 119 forks source link

[Question] How can I solve 'token is not ready' problem? #247

Closed jatu-studiobox closed 1 year ago

jatu-studiobox commented 1 year ago

Hi,

I have some problem about 'token'. I use 'email & password' authentication with Firebase. If ESP32 don't working for a while, and then came back to work with RTDB. There is an error 'token is not ready'.

Below is my inititialize connect to Firebase

` void initFirebase() { / Assign the api key (required) / firebaseConfig.api_key = FIREBASE_API_KEY;

/* Assign the user sign in credentials */
auth.user.email = FIREBASE_USER_EMAIL;
auth.user.password = FIREBASE_USER_PASSWORD;

/* Assign the RTDB URL (required) */
firebaseConfig.database_url = FIREBASE_DATABASE_URL;

/* Assign the callback function for the long running token generation task */
firebaseConfig.token_status_callback = tokenStatusCallback;  // see addons/TokenHelper.h

// Begin connect firebase
Firebase.begin(&firebaseConfig, &auth);
Firebase.reconnectWiFi(true);

// Set max retry
Firebase.setMaxRetry(firebaseDataOperator, 3);

// Get Configuration from database
getConfiguration();
// Check have error firebase
if (has_error) {
    displayMessage();
    delay(1500);
}

// Set Request Reset value to No Request
setFirebaseRequestResetToNo();
// Check have error firebase
if (has_error) {
    displayMessage();
    delay(1500);
}

// Stream tb-channel
if (!Firebase.beginMultiPathStream(firebaseDataChannelObserver, channelPath, childOpenChannelPath, childChannelSize)) {
    Serial.println(F("------------------------------------"));
    Serial.println(F("Can't begin Channels stream connection...\nReason :"));
    Serial.println(firebaseDataChannelObserver.errorReason());
    Serial.println(F("------------------------------------"));
    Serial.println();
}
Firebase.setMultiPathStreamCallback(firebaseDataChannelObserver, streamChannelCallback, streamChannelTimeoutCallback);

}`

Please advise me, how can I solve this problem?

mobizt commented 1 year ago

The library uses token for authentication except for legacy token (database secret) which will be expired in 1 hour after created and need to be refresh.

You should run Firebase.ready() in loop to handle the authentication process or before whenever you want to access the Firebase services, like this.

https://github.com/mobizt/Firebase-ESP32/blob/19d4e3efd83c3fb61da0da8bb434174ceae202c4/examples/Basic/Basic.ino#L134-L139

The return value from Firebase.ready() is the authentication status.

Please follow the examples first and read the comments in the examples for the details and important information.

jatu-studiobox commented 1 year ago

Thank you very much for your help. :)