Skip to content

Commit f2609f0

Browse files
refactor: phone auth provider
1 parent bbce389 commit f2609f0

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/AuthServiceError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public enum AuthServiceError: LocalizedError {
3636
case invalidCredentials(String)
3737
case signInFailed(underlying: Error)
3838
case accountMergeConflict(context: AccountMergeConflictContext)
39+
case invalidPhoneAuthenticationArguments(String)
3940
case providerNotFound(String)
4041

4142
public var errorDescription: String? {
@@ -58,6 +59,8 @@ public enum AuthServiceError: LocalizedError {
5859
return context.errorDescription
5960
case let .providerNotFound(description):
6061
return description
62+
case let .invalidPhoneAuthenticationArguments(description):
63+
return description
6164
}
6265
}
6366
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderAuthUI.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ public class PhoneAuthProviderAuthUI: @preconcurrency PhoneAuthProviderAuthUIPro
3030
@MainActor public func authButton() -> AnyView {
3131
AnyView(PhoneAuthButtonView())
3232
}
33-
34-
@MainActor public func createAuthCredential() async throws -> AuthCredential {
35-
guard let verificationID = storedVerificationID,
36-
let verificationCode = storedVerificationCode else {
37-
fatalError("Phone authentication requires verifyPhoneNumber to be called first")
38-
}
39-
return PhoneAuthProvider.provider().credential(
40-
withVerificationID: verificationID,
41-
verificationCode: verificationCode
42-
)
43-
}
4433

4534
@MainActor public func verifyPhoneNumber(phoneNumber: String) async throws -> VerificationID {
4635
return try await withCheckedThrowingContinuation { continuation in
@@ -54,4 +43,20 @@ public class PhoneAuthProviderAuthUI: @preconcurrency PhoneAuthProviderAuthUIPro
5443
}
5544
}
5645
}
46+
47+
// Set verification details before calling signIn
48+
public func setVerificationDetails(verificationID: String, verificationCode: String) {
49+
self.storedVerificationID = verificationID
50+
self.storedVerificationCode = verificationCode
51+
}
52+
53+
@MainActor public func createAuthCredential() async throws -> AuthCredential {
54+
guard let verificationID = storedVerificationID,
55+
let verificationCode = storedVerificationCode else {
56+
throw AuthServiceError.invalidPhoneAuthenticationArguments("please call setVerificationDetails() before creating Phone Auth credential")
57+
}
58+
59+
return PhoneAuthProvider.provider()
60+
.credential(withVerificationID: verificationID, verificationCode: verificationCode)
61+
}
5762
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Views/PhoneAuthView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public struct PhoneAuthView {
3131
@State private var showVerificationCodeInput = false
3232
@State private var verificationCode = ""
3333
@State private var verificationID = ""
34+
private let phoneProvider = PhoneAuthProviderAuthUI()
3435

3536
public init() {}
3637
}
@@ -52,7 +53,7 @@ extension PhoneAuthView: View {
5253
Button(action: {
5354
Task {
5455
do {
55-
let id = try await authService.verifyPhoneNumber(phoneNumber: phoneNumber)
56+
let id = try await phoneProvider.verifyPhoneNumber(phoneNumber: phoneNumber)
5657
verificationID = id
5758
showVerificationCodeInput = true
5859
} catch {
@@ -82,10 +83,11 @@ extension PhoneAuthView: View {
8283
Button(action: {
8384
Task {
8485
do {
85-
try await authService.signInWithPhoneNumber(
86+
phoneProvider.setVerificationDetails(
8687
verificationID: verificationID,
8788
verificationCode: verificationCode
8889
)
90+
try await authService.signInWithProvider(phoneProvider)
8991
} catch {
9092
errorMessage = authService.string.localizedErrorMessage(for: error)
9193
}

0 commit comments

Comments
 (0)