6
6
#include " Firestore/core/src/util/status.h"
7
7
#include " app/src/function_registry.h"
8
8
#include " app/src/reference_counted_future_impl.h"
9
+ #include " firebase/auth/types.h"
9
10
#include " firebase/firestore/firestore_errors.h"
10
11
#include " firestore/src/common/futures.h"
11
12
#include " firestore/src/common/hard_assert_common.h"
@@ -18,9 +19,39 @@ using auth::CredentialChangeListener;
18
19
using auth::Token;
19
20
using auth::TokenListener;
20
21
using auth::User;
22
+ using firebase::auth::AuthError;
21
23
using util::Status;
22
24
using util::StatusOr;
23
25
26
+ /* *
27
+ * Takes an integer that represents an `AuthError` enum value, and returns a
28
+ * `firestore::Error` that best describes the given `AuthError`.
29
+ */
30
+ Error FirestoreErrorFromAuthError (int error) {
31
+ switch (error) {
32
+ case AuthError::kAuthErrorNone :
33
+ return kErrorNone ;
34
+ case AuthError::kAuthErrorUnimplemented :
35
+ return kErrorUnimplemented ;
36
+ case AuthError::kAuthErrorFailure :
37
+ return kErrorInternal ;
38
+ case AuthError::kAuthErrorNetworkRequestFailed :
39
+ return kErrorUnavailable ;
40
+ case AuthError::kAuthErrorCancelled :
41
+ return kErrorCancelled ;
42
+ case AuthError::kAuthErrorInvalidCustomToken :
43
+ case AuthError::kAuthErrorInvalidCredential :
44
+ case AuthError::kAuthErrorUserDisabled :
45
+ case AuthError::kAuthErrorUserNotFound :
46
+ case AuthError::kAuthErrorInvalidUserToken :
47
+ case AuthError::kAuthErrorUserTokenExpired :
48
+ case AuthError::kAuthErrorNoSignedInUser :
49
+ return kErrorUnauthenticated ;
50
+ default :
51
+ return kErrorUnknown ;
52
+ }
53
+ }
54
+
24
55
/* *
25
56
* Returns a Future that, when completed, will contain the token for the
26
57
* current user or an error. An empty token means that the current user is
@@ -58,12 +89,9 @@ User GetCurrentUser(App& app) {
58
89
StatusOr<Token> ConvertToken (const Future<std::string>& future, App& app) {
59
90
if (future.error () != Error::kErrorOk ) {
60
91
// `AuthError` is a different error domain from go/canonical-codes that
61
- // `Status` uses, so it can't be converted directly. Instead, use
62
- // `kErrorUnknown` in the `Status` because the error code from the future
63
- // is "from a different error domain".
64
- // TODO(b/174485290) Map `AuthError` values to Firestore `Error` values more
65
- // intelligently so as to enable retries when appropriate.
66
- return Status (Error::kErrorUnknown ,
92
+ // `Status` uses. We map `AuthError` values to Firestore `Error` values in
93
+ // order to be able to perform retries when appropriate.
94
+ return Status (FirestoreErrorFromAuthError (future.error ()),
67
95
std::string (future.error_message ()) + " (AuthError " +
68
96
std::to_string (future.error ()) + " )" );
69
97
}
@@ -76,8 +104,10 @@ StatusOr<Token> ConvertToken(const Future<std::string>& future, App& app) {
76
104
// the `listener` with the error. If the current token generation is higher
77
105
// than `expected_generation`, will invoke the `listener` with "aborted"
78
106
// error. `future_token` must be a completed future.
79
- void OnToken (const Future<std::string>& future_token, App& app,
80
- int token_generation, const TokenListener& listener,
107
+ void OnToken (const Future<std::string>& future_token,
108
+ App& app,
109
+ int token_generation,
110
+ const TokenListener& listener,
81
111
int expected_generation) {
82
112
SIMPLE_HARD_ASSERT (
83
113
future_token.status () == FutureStatus::kFutureStatusComplete ,
@@ -98,7 +128,8 @@ void OnToken(const Future<std::string>& future_token, App& app,
98
128
} // namespace
99
129
100
130
FirebaseCppCredentialsProvider::FirebaseCppCredentialsProvider (App& app)
101
- : contents_(std::make_shared<Contents>(app)) {}
131
+ : contents_(std::make_shared<Contents>(app)) {
132
+ }
102
133
103
134
FirebaseCppCredentialsProvider::~FirebaseCppCredentialsProvider () {
104
135
RemoveAuthStateListener ();
0 commit comments