Skip to content

Commit 454f399

Browse files
authored
[Auth] Use result type to wrap completion handlers internally (#13563)
1 parent b9fff57 commit 454f399

File tree

5 files changed

+55
-56
lines changed

5 files changed

+55
-56
lines changed

FirebaseAuth/Sources/Swift/Auth/Auth.swift

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ extension Auth: AuthInterop {
300300
Task {
301301
do {
302302
let response = try await self.backend.call(with: request)
303-
Auth.wrapMainAsync(callback: completion, withParam: response.signinMethods, error: nil)
303+
Auth.wrapMainAsync(callback: completion, with: .success(response.signinMethods))
304304
} catch {
305-
Auth.wrapMainAsync(callback: completion, withParam: nil, error: error)
305+
Auth.wrapMainAsync(callback: completion, with: .failure(error))
306306
}
307307
}
308308
}
@@ -365,9 +365,9 @@ extension Auth: AuthInterop {
365365
withEmail: email,
366366
password: password
367367
)
368-
decoratedCallback(authData, nil)
368+
decoratedCallback(.success(authData))
369369
} catch {
370-
decoratedCallback(nil, error)
370+
decoratedCallback(.failure(error))
371371
}
372372
}
373373
}
@@ -463,9 +463,9 @@ extension Auth: AuthInterop {
463463
do {
464464
let authData = try await self.internalSignInAndRetrieveData(withCredential: credential,
465465
isReauthentication: false)
466-
decoratedCallback(authData, nil)
466+
decoratedCallback(.success(authData))
467467
} catch {
468-
decoratedCallback(nil, error)
468+
decoratedCallback(.failure(error))
469469
}
470470
}
471471
}
@@ -544,9 +544,9 @@ extension Auth: AuthInterop {
544544
withCredential: credential,
545545
isReauthentication: false
546546
)
547-
decoratedCallback(authData, nil)
547+
decoratedCallback(.success(authData))
548548
} catch {
549-
decoratedCallback(nil, error)
549+
decoratedCallback(.failure(error))
550550
}
551551
}
552552
}
@@ -642,9 +642,9 @@ extension Auth: AuthInterop {
642642
do {
643643
let authData = try await self.internalSignInAndRetrieveData(withCredential: credential,
644644
isReauthentication: false)
645-
decoratedCallback(authData, nil)
645+
decoratedCallback(.success(authData))
646646
} catch {
647-
decoratedCallback(nil, error)
647+
decoratedCallback(.failure(error))
648648
}
649649
}
650650
}
@@ -710,7 +710,7 @@ extension Auth: AuthInterop {
710710
let decoratedCallback = self.signInFlowAuthDataResultCallback(byDecorating: completion)
711711
if let currentUser = self._currentUser, currentUser.isAnonymous {
712712
let result = AuthDataResult(withUser: currentUser, additionalUserInfo: nil)
713-
decoratedCallback(result, nil)
713+
decoratedCallback(.success(result))
714714
return
715715
}
716716
let request = SignUpNewUserRequest(requestConfiguration: self.requestConfiguration)
@@ -728,10 +728,11 @@ extension Auth: AuthInterop {
728728
profile: nil,
729729
username: nil,
730730
isNewUser: true)
731-
decoratedCallback(AuthDataResult(withUser: user, additionalUserInfo: additionalUserInfo),
732-
nil)
731+
decoratedCallback(
732+
.success(AuthDataResult(withUser: user, additionalUserInfo: additionalUserInfo))
733+
)
733734
} catch {
734-
decoratedCallback(nil, error)
735+
decoratedCallback(.failure(error))
735736
}
736737
}
737738
}
@@ -790,10 +791,11 @@ extension Auth: AuthInterop {
790791
profile: nil,
791792
username: nil,
792793
isNewUser: response.isNewUser)
793-
decoratedCallback(AuthDataResult(withUser: user, additionalUserInfo: additionalUserInfo),
794-
nil)
794+
decoratedCallback(
795+
.success(AuthDataResult(withUser: user, additionalUserInfo: additionalUserInfo))
796+
)
795797
} catch {
796-
decoratedCallback(nil, error)
798+
decoratedCallback(.failure(error))
797799
}
798800
}
799801
}
@@ -866,7 +868,7 @@ extension Auth: AuthInterop {
866868
action: AuthRecaptchaAction.signUpPassword) { response, error in
867869
if let error {
868870
DispatchQueue.main.async {
869-
decoratedCallback(nil, error)
871+
decoratedCallback(.failure(error))
870872
}
871873
return
872874
}
@@ -881,7 +883,8 @@ extension Auth: AuthInterop {
881883

882884
func internalCreateUserWithEmail(request: SignUpNewUserRequest,
883885
inResponse: SignUpNewUserResponse? = nil,
884-
decoratedCallback: @escaping (AuthDataResult?, Error?) -> Void) {
886+
decoratedCallback: @escaping (Result<AuthDataResult, Error>)
887+
-> Void) {
885888
Task {
886889
do {
887890
var response: SignUpNewUserResponse
@@ -900,11 +903,11 @@ extension Auth: AuthInterop {
900903
profile: nil,
901904
username: nil,
902905
isNewUser: true)
903-
decoratedCallback(AuthDataResult(withUser: user,
904-
additionalUserInfo: additionalUserInfo),
905-
nil)
906+
decoratedCallback(
907+
.success(AuthDataResult(withUser: user, additionalUserInfo: additionalUserInfo))
908+
)
906909
} catch {
907-
decoratedCallback(nil, error)
910+
decoratedCallback(.failure(error))
908911
}
909912
}
910913
}
@@ -1009,9 +1012,9 @@ extension Auth: AuthInterop {
10091012
let actionCodeInfo = ActionCodeInfo(withOperation: operation,
10101013
email: email,
10111014
newEmail: response.verifiedEmail)
1012-
Auth.wrapMainAsync(callback: completion, withParam: actionCodeInfo, error: nil)
1015+
Auth.wrapMainAsync(callback: completion, with: .success(actionCodeInfo))
10131016
} catch {
1014-
Auth.wrapMainAsync(callback: completion, withParam: nil, error: error)
1017+
Auth.wrapMainAsync(callback: completion, with: .failure(error))
10151018
}
10161019
}
10171020
}
@@ -2238,23 +2241,19 @@ extension Auth: AuthInterop {
22382241
/// Invoked asynchronously on the main thread in the future.
22392242
/// - Returns: Returns a block that updates the current user.
22402243
func signInFlowAuthDataResultCallback(byDecorating callback:
2241-
((AuthDataResult?, Error?) -> Void)?) -> (AuthDataResult?, Error?) -> Void {
2242-
let authDataCallback: (((AuthDataResult?, Error?) -> Void)?, AuthDataResult?, Error?) -> Void =
2243-
{ callback, result, error in
2244-
Auth.wrapMainAsync(callback: callback, withParam: result, error: error)
2245-
}
2246-
return { authResult, error in
2247-
if let error {
2248-
authDataCallback(callback, nil, error)
2249-
return
2250-
}
2251-
do {
2252-
try self.updateCurrentUser(authResult?.user, byForce: false, savingToDisk: true)
2253-
} catch {
2254-
authDataCallback(callback, nil, error)
2255-
return
2244+
((AuthDataResult?, Error?) -> Void)?) -> (Result<AuthDataResult, Error>) -> Void {
2245+
return { result in
2246+
switch result {
2247+
case let .success(authResult):
2248+
do {
2249+
try self.updateCurrentUser(authResult.user, byForce: false, savingToDisk: true)
2250+
Auth.wrapMainAsync(callback: callback, with: .success(authResult))
2251+
} catch {
2252+
Auth.wrapMainAsync(callback: callback, with: .failure(error))
2253+
}
2254+
case let .failure(error):
2255+
Auth.wrapMainAsync(callback: callback, with: .failure(error))
22562256
}
2257-
authDataCallback(callback, authResult, nil)
22582257
}
22592258
}
22602259

@@ -2278,11 +2277,12 @@ extension Auth: AuthInterop {
22782277
}
22792278

22802279
class func wrapMainAsync<T: Any>(callback: ((T?, Error?) -> Void)?,
2281-
withParam param: T?,
2282-
error: Error?) -> Void {
2283-
if let callback {
2284-
DispatchQueue.main.async {
2285-
callback(param, error)
2280+
with result: Result<T, Error>) -> Void {
2281+
guard let callback else { return }
2282+
DispatchQueue.main.async {
2283+
switch result {
2284+
case let .success(success): callback(success, nil)
2285+
case let .failure(error): callback(nil, error)
22862286
}
22872287
}
22882288
}

FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ import Foundation
265265
/// - Parameter phoneNumber: The phone number to be verified.
266266
/// - Parameter callback: The callback to be invoked on the global work queue when the flow is
267267
/// finished.
268-
func verifyClAndSendVerificationCode(toPhoneNumber phoneNumber: String,
269-
retryOnInvalidAppCredential: Bool,
270-
uiDelegate: AuthUIDelegate?,
271-
auditFallback: Bool = false) async throws
268+
private func verifyClAndSendVerificationCode(toPhoneNumber phoneNumber: String,
269+
retryOnInvalidAppCredential: Bool,
270+
uiDelegate: AuthUIDelegate?,
271+
auditFallback: Bool = false) async throws
272272
-> String? {
273273
let codeIdentity = try await verifyClient(withUIDelegate: uiDelegate)
274274
let request = SendVerificationCodeRequest(phoneNumber: phoneNumber,
@@ -350,7 +350,7 @@ import Foundation
350350
action: .mfaSmsSignIn
351351
)
352352
let response = try await auth.backend.call(with: request)
353-
return response.responseInfo?.sessionInfo
353+
return response.responseInfo.sessionInfo
354354
}
355355
} catch {
356356
// For Audit fallback only after rCE check failed
@@ -419,9 +419,8 @@ import Foundation
419419
MFAEnrollmentID: session.multiFactorInfo?.uid,
420420
signInInfo: startMFARequestInfo,
421421
requestConfiguration: auth.requestConfiguration)
422-
423422
let response = try await auth.backend.call(with: request)
424-
return response.responseInfo?.sessionInfo
423+
return response.responseInfo.sessionInfo
425424
}
426425
} catch {
427426
return try await handleVerifyErrorWithRetry(

FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/StartMFASignInResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Foundation
1616

1717
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
1818
struct StartMFASignInResponse: AuthRPCResponse {
19-
var responseInfo: AuthProtoStartMFAPhoneResponseInfo?
19+
let responseInfo: AuthProtoStartMFAPhoneResponseInfo
2020

2121
init(dictionary: [String: AnyHashable]) throws {
2222
if let data = dictionary["phoneResponseInfo"] as? [String: AnyHashable] {

FirebaseAuth/Sources/Swift/Backend/RPC/SendVerificationTokenResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import Foundation
1616

1717
struct SendVerificationCodeResponse: AuthRPCResponse {
18-
var verificationID: String?
18+
let verificationID: String?
1919

2020
init(dictionary: [String: AnyHashable]) throws {
2121
verificationID = dictionary["sessionInfo"] as? String

FirebaseAuth/Sources/Swift/MultiFactor/MultiFactorResolver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ import Foundation
8080
let result = AuthDataResult(withUser: user, additionalUserInfo: nil)
8181
let decoratedCallback = self.auth
8282
.signInFlowAuthDataResultCallback(byDecorating: completion)
83-
decoratedCallback(result, nil)
83+
decoratedCallback(.success(result))
8484
} catch {
8585
if let completion {
8686
completion(nil, error)

0 commit comments

Comments
 (0)