-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Auth] Use result type to wrap completion handlers internally #13563
base: main
Are you sure you want to change the base?
Conversation
FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift
Outdated
Show resolved
Hide resolved
@@ -236,7 +236,7 @@ import Foundation | |||
retryOnInvalidAppCredential: Bool, | |||
multiFactorSession session: MultiFactorSession?, | |||
uiDelegate: AuthUIDelegate?) async throws | |||
-> String? { | |||
-> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This return type bubbles up to the public API, which we need to make non-optional if possible, so this is why this is being changed to non-optional String.
@@ -16,7 +16,7 @@ import Foundation | |||
|
|||
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) | |||
struct StartMFASignInResponse: AuthRPCResponse { | |||
var responseInfo: AuthProtoStartMFAPhoneResponseInfo? | |||
let responseInfo: AuthProtoStartMFAPhoneResponseInfo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diff should be a no-op in practice because the initializer will throw if it could not unwrap the value being assigned to responseInfo
.
let verificationID: String | ||
|
||
init(dictionary: [String: AnyHashable]) throws { | ||
verificationID = dictionary["sessionInfo"] as? String | ||
guard let verificationID = dictionary["sessionInfo"] as? String else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, this should be non-optional. I'm adding the error for cases where it's not. Are there success cases where verificationID should be nil
?
guard let sessionInfo = response.phoneSessionInfo?.sessionInfo else { | ||
throw AuthErrorUtils.error(code: .missingMultiFactorInfo) | ||
} | ||
return sessionInfo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and below, we need to unwrap the underlying session info which is current an optional:
Lines 17 to 23 in 380a861
struct AuthProtoStartMFAPhoneResponseInfo: AuthProto { | |
var sessionInfo: String? | |
init(dictionary: [String: AnyHashable]) { | |
sessionInfo = dictionary["sessionInfo"] as? String | |
} | |
} |
Throwing an error is new behavior here. I think this may be ok as long there aren't success cases where the sessionInfo should be nil
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior changes made here map to this public API:
firebase-ios-sdk/FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift
Lines 97 to 109 in 380a861
/// Verify ownership of the second factor phone number by the current user. | |
/// - Parameter phoneNumber: The phone number to be verified. | |
/// - Parameter uiDelegate: An object used to present the SFSafariViewController. The object is | |
/// retained by this method until the completion block is executed. | |
/// - Parameter multiFactorSession: A session to identify the MFA flow. For enrollment, this | |
/// identifies the user trying to enroll. For sign-in, this identifies that the user already | |
/// passed the first factor challenge. | |
/// - Returns: The verification ID | |
@available(iOS 13, tvOS 13, macOS 10.15, watchOS 8, *) | |
open func verifyPhoneNumber(_ phoneNumber: String, | |
uiDelegate: AuthUIDelegate? = nil, | |
multiFactorSession: MultiFactorSession? = nil) async throws | |
-> String { |
Is there a valid success case where the returned String should be nil
(or an empty string) and the error is also non-nil? This PR enforces that it is either one or the other (non-optional String OR error).
Follow-up to #13561
Use Swift's
Result
type enum to prevent cases where a completion handler is called with both non-nil or both nil.#no-changelog