Skip to content

Commit

Permalink
Merge pull request #17 from MrAsterisco/16-change-request-all-failure…
Browse files Browse the repository at this point in the history
…s-during-automatic-linking-should-be-considered

All failures during automatic linking are mapped to the new anonymous user reset
  • Loading branch information
MrAsterisco authored Apr 14, 2024
2 parents 2c350b2 + 633f4c7 commit b3a63b6
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions RxFireAuth/Classes/UserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,14 @@ extension UserManager {
let firebaseCredentials = credentials.asAuthCredentials()

var oldUserId: String?
let signInCompletionHandler: (Error?) -> Void = { (error) in
let signInCompletionHandler: (Bool, Error?) -> Void = { (fromAutomaticLinking, error) in
guard !disposable.isDisposed else { return }
if let error = error {
if (error as NSError).code == AuthErrorCode.invalidCredential.rawValue {
if fromAutomaticLinking {
observer(.failure(UserInternalError.automaticLinkingFailed(oldUserId: oldUserId, internalError: error)))
} else {
observer(.failure(map(error: error)))
}
observer(.failure(map(error: error)))
} else if let newUser = Auth.auth().currentUser {
observer(
.success(
Expand Down Expand Up @@ -534,6 +535,7 @@ extension UserManager {
if credentials.isReusable {
self.signIn(
with: firebaseCredentials,
fromAutomaticLinking: true,
in: disposable,
completionHandler: signInCompletionHandler
)
Expand All @@ -551,14 +553,15 @@ extension UserManager {
}

/// Linking succeeded or failed with a different error, so we return.
signInCompletionHandler(error)
signInCompletionHandler(false, error)
}
}
} else {
/// There's nobody logged-in.
/// We'll go ahead and sign in with the authentication method.
self.signIn(
with: firebaseCredentials,
fromAutomaticLinking: false,
in: disposable,
completionHandler: signInCompletionHandler
)
Expand Down Expand Up @@ -641,16 +644,17 @@ extension UserManager {
///
/// - parameters:
/// - credentials: The credential to use to sign in.
/// - fromAutomaticLinking: A bool indicating whether this invocation comes as a result of automatic linking.
/// - disposable: A disposable that controls the life of this operation.
/// - completionHandler: A completion handler to call when done.
func signIn(with credentials: AuthCredential, in disposable: Cancelable, completionHandler: @escaping (Error?) -> Void) {
func signIn(with credentials: AuthCredential, fromAutomaticLinking: Bool, in disposable: Cancelable, completionHandler: @escaping (Bool, Error?) -> Void) {
Auth.auth().signIn(with: credentials) { (_, error) in
guard !disposable.isDisposed else { return }

if let error = error {
completionHandler(error)
completionHandler(fromAutomaticLinking, error)
} else {
completionHandler(nil)
completionHandler(fromAutomaticLinking, nil)
}
}
}
Expand Down

0 comments on commit b3a63b6

Please sign in to comment.