Skip to content

Commit c85b330

Browse files
DellaBittaa-maurice
authored andcommitted
On iOS for nonce-based Apple Sign In link failures where the Apple account is already linked to a different Firebase account, returns an Updated Credential that may be used to login to the Apple-linked account.
PiperOrigin-RevId: 311328231
1 parent 49a16ff commit c85b330

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

auth/src/include/firebase/auth/credential.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class Credential {
6464
friend class PlayGamesAuthProvider;
6565
friend class TwitterAuthProvider;
6666
friend class YahooAuthProvider;
67+
friend class ServiceUpdatedCredentialProvider;
6768
/// @endcond
6869
#endif // !SWIG
6970

auth/src/include/firebase/auth/user.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ struct AdditionalUserInfo {
120120
/// Most likely a hierarchical key-value mapping, like a parsed JSON file.
121121
/// Note we use map instead of unordered_map to support older compilers.
122122
std::map<Variant, Variant> profile;
123+
124+
/// On a nonce-based credential link failure where the user has already linked
125+
/// to the provider, the Firebase auth service may provide an updated
126+
/// Credential. If is_valid returns true on this credential, then it may be
127+
/// passed to a new firebase::auth::Auth::SignInWithCredential request to sign
128+
/// the user in with the provider.
129+
Credential updated_credential;
123130
};
124131

125132
/// @brief Metadata corresponding to a Firebase user.

auth/src/ios/auth_ios.mm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#import "FIRAuthDataResult.h"
1919
#import "FIRAuthErrors.h"
2020
#import "FIROptions.h"
21+
#import "FIROAuthCredential.h"
2122

2223
#include "app/src/app_ios.h"
2324
#include "app/src/assert.h"
@@ -118,6 +119,18 @@ @implementation FIRCPPAuthListenerHandle
118119
// kAuthErrorEmailChangeNeedsVerification},
119120
};
120121

122+
// A provider that wraps a Credential returned from the Firebase iOS SDK which may occur when a
123+
// nonce based link failure occurs due to a pre-existing account already linked with a Provider.
124+
// This credential may be used to attempt to sign into Firebase using that account.
125+
class ServiceUpdatedCredentialProvider {
126+
public:
127+
// Construct a Credential given a preexisting FIRAuthCredential wrapped by a
128+
// FIRAuthCredentialPointer.
129+
static Credential GetCredential(FIRAuthCredentialPointer* impl) {
130+
return Credential(impl);
131+
}
132+
};
133+
121134
template<typename T>
122135
struct ListenerHandleHolder {
123136
explicit ListenerHandleHolder(T handle) : handle(handle) {}
@@ -315,6 +328,13 @@ void SignInResultCallback(FIRAuthDataResult *_Nullable auth_result, NSError *_Nu
315328
util::NSDictionaryToStdMap(auth_result.additionalUserInfo.profile, &result.info.profile);
316329
}
317330

331+
if (error.userInfo != nullptr) {
332+
if (error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey] != nullptr) {
333+
result.info.updated_credential = ServiceUpdatedCredentialProvider::GetCredential(
334+
new FIRAuthCredentialPointer(error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey]));
335+
}
336+
}
337+
318338
ReferenceCountedFutureImpl &futures = auth_data->future_impl;
319339
futures.CompleteWithResult(handle, AuthErrorFromNSError(error),
320340
util::NSStringToString(error.localizedDescription).c_str(), result);

0 commit comments

Comments
 (0)