Skip to content

Commit 49c633c

Browse files
chkuang-ga-maurice
authored andcommitted
Properly warn if auth token is rejected by the RT DB server
This aligns with Android implementation, which would warn when the token is rejected for more than 3 time. https://cs.corp.google.com/piper///depot/google3/java/com/google/android/gmscore/dev/client/firebase-database-connection/src/com/google/firebase/database/connection/PersistentConnectionImpl.java?l=844 For desktop implementation, it would attempt to reconnect immediately. The retry windows should be implemented in b/123780315. PiperOrigin-RevId: 246433502
1 parent 311c8c2 commit 49c633c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

database/src/desktop/connection/persistent_connection.cc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ const char* PersistentConnection::kServerDataTag = "t";
8686
const char* PersistentConnection::kServerDataWarnings = "w";
8787
const char* PersistentConnection::kServerResponseData = "d";
8888

89+
int PersistentConnection::kInvalidAuthTokenThreshold = 3;
90+
8991
compat::Atomic<uint32_t> PersistentConnection::next_log_id_(0);
9092

9193
// Util function to print QuerySpec in debug logs.
@@ -108,6 +110,7 @@ PersistentConnection::PersistentConnection(
108110
realtime_(nullptr),
109111
connection_state_(kDisconnected),
110112
is_first_connection_(true),
113+
invalid_auth_token_count(0),
111114
next_request_id_(0),
112115
force_auth_refresh_(false),
113116
next_listen_id_(0),
@@ -926,6 +929,7 @@ void PersistentConnection::HandleAuthTokenResponse(const Variant& message,
926929
std::string status = GetStringValue(message, kRequestStatus);
927930

928931
if (status == kRequestStatusOk) {
932+
invalid_auth_token_count = 0;
929933
event_handler_->OnAuthStatus(true);
930934
LogDebug("%s Authentication success", log_id_.c_str());
931935

@@ -944,8 +948,25 @@ void PersistentConnection::HandleAuthTokenResponse(const Variant& message,
944948
status.c_str(), reason.c_str());
945949
realtime_->Close();
946950

947-
// TODO(chkuang): schedule another time to retrieve and send another auth
948-
// token
951+
Error error = StatusStringToErrorCode(status);
952+
if (error == kErrorInvalidToken) {
953+
// We'll wait a couple times before logging the warning / increasing the
954+
// retry period since oauth tokens will report as "invalid" if they're
955+
// just expired. Plus there may be transient issues that resolve
956+
// themselves.
957+
++invalid_auth_token_count;
958+
959+
if (invalid_auth_token_count >= kInvalidAuthTokenThreshold) {
960+
// TODO(chkuang): Maximize retry interval (after retry is properly
961+
// implemented).
962+
LogWarning(
963+
"Provided authentication credentials are invalid. This indicates "
964+
"your FirebaseApp instance was not initialized correctly. Make "
965+
"sure your google-services.json file has the correct firebase_url "
966+
"and api_key. You can re-download google-services.json from "
967+
"https://console.firebase.google.com/.");
968+
}
969+
}
949970
}
950971
}
951972

database/src/desktop/connection/persistent_connection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ class PersistentConnection : public ConnectionEventHandler {
455455
static const char* kServerDataWarnings;
456456
static const char* kServerResponseData;
457457

458+
static int kInvalidAuthTokenThreshold;
459+
458460
// Log id. Unique for each persistent connection.
459461
static compat::Atomic<uint32_t> next_log_id_;
460462
std::string log_id_;
@@ -489,6 +491,9 @@ class PersistentConnection : public ConnectionEventHandler {
489491
// Session
490492
std::string last_session_id_;
491493

494+
// Number of time when auth token is rejected by the server
495+
int invalid_auth_token_count;
496+
492497
// Request/Response
493498
uint64_t next_request_id_;
494499
std::map<uint64_t, RequestDataPtr> request_map_;

0 commit comments

Comments
 (0)