From 308acc09bfaf6dabf4b6d5f5e39f33854df8ce34 Mon Sep 17 00:00:00 2001 From: rsgowman Date: Wed, 21 Mar 2018 11:04:40 -0400 Subject: Change CredentialsProvider::TokenListener to use StatusOr (#945) * Change CredentialsProvider::TokenListener to use StatusOr Rather than a token plus error code/msg. * Eliminate the concept of an invalid Token Instead, we'll just use StatusOr. Note that unauthenticated tokens are handled as a special case; they're created via: Token::Unauthenticated() and are otherwise "valid", though attempting to retrieve the raw token on one of these tokens will cause an assertion failure. --- .../auth/firebase_credentials_provider_apple.mm | 56 ++++++++++++++-------- 1 file changed, 35 insertions(+), 21 deletions(-) (limited to 'Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm') diff --git a/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm b/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm index 1babe82..2bd3acc 100644 --- a/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm +++ b/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm @@ -23,6 +23,9 @@ #include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" #include "Firestore/core/src/firebase/firestore/util/string_apple.h" +// NB: This is also defined in Firestore/Source/Public/FIRFirestoreErrors.h +NSString* const FIRFirestoreErrorDomain = @"FIRFirestoreErrorDomain"; + namespace firebase { namespace firestore { namespace auth { @@ -84,27 +87,38 @@ void FirebaseCredentialsProvider::GetToken(bool force_refresh, int initial_user_counter = contents_->user_counter; std::weak_ptr weak_contents = contents_; - void (^get_token_callback)(NSString*, NSError*) = ^( - NSString* _Nullable token, NSError* _Nullable error) { - std::shared_ptr contents = weak_contents.lock(); - if (!contents) { - return; - } - - std::unique_lock lock(contents->mutex); - if (initial_user_counter != contents->user_counter) { - // Cancel the request since the user changed while the request was - // outstanding so the response is likely for a previous user (which - // user, we can't be sure). - completion(Token::Invalid(), FirestoreErrorCode::Aborted, - "getToken aborted due to user change."); - } else { - completion( - Token{util::MakeStringView(token), contents->current_user}, - error == nil ? FirestoreErrorCode::Ok : error.code, - error == nil ? "" : util::MakeStringView(error.localizedDescription)); - } - }; + void (^get_token_callback)(NSString*, NSError*) = + ^(NSString* _Nullable token, NSError* _Nullable error) { + std::shared_ptr contents = weak_contents.lock(); + if (!contents) { + return; + } + + std::unique_lock lock(contents->mutex); + if (initial_user_counter != contents->user_counter) { + // Cancel the request since the user changed while the request was + // outstanding so the response is likely for a previous user (which + // user, we can't be sure). + completion(util::Status(FirestoreErrorCode::Aborted, + "getToken aborted due to user change.")); + } else { + if (error == nil) { + if (token != nil) { + completion( + Token{util::MakeStringView(token), contents->current_user}); + } else { + completion(Token::Unauthenticated()); + } + } else { + FirestoreErrorCode error_code = FirestoreErrorCode::Unknown; + if (error.domain == FIRFirestoreErrorDomain) { + error_code = static_cast(error.code); + } + completion(util::Status( + error_code, util::MakeStringView(error.localizedDescription))); + } + } + }; [contents_->app getTokenForcingRefresh:force_refresh withCallback:get_token_callback]; -- cgit v1.2.3