Skip to content

Commit 0b7037f

Browse files
fix: allow user to set their own callback for phone auth
1 parent b634cba commit 0b7037f

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/AuthService+Phone.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@
2222
import FirebaseAuthSwiftUI
2323

2424
public extension AuthService {
25+
/// Register phone sign-in with default behavior (navigates to enter phone number view)
2526
@discardableResult
2627
func withPhoneSignIn() -> AuthService {
27-
registerProvider(providerWithButton: PhoneAuthProviderAuthUI())
28+
return withPhoneSignIn { [weak self] in
29+
self?.navigator.push(.enterPhoneNumber)
30+
}
31+
}
32+
33+
/// Register phone sign-in with custom behavior
34+
@discardableResult
35+
func withPhoneSignIn(onTap: @escaping @MainActor () -> Void) -> AuthService {
36+
registerProvider(providerWithButton: PhoneAuthProviderAuthUI(onTap: onTap))
2837
return self
2938
}
3039
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderAuthUI.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ public class PhoneAuthProviderAuthUI: AuthProviderUI {
2525
public var provider: AuthProviderSwift { typedProvider }
2626
public let id: String = "phone"
2727

28-
public init() {
28+
// Callback for when the phone auth button is tapped
29+
private let onTap: @MainActor () -> Void
30+
31+
public init(onTap: @escaping @MainActor () -> Void) {
2932
typedProvider = PhoneProviderSwift()
33+
self.onTap = onTap
3034
}
3135

3236
@MainActor public func authButton() -> AnyView {
33-
AnyView(PhoneAuthButtonView())
37+
AnyView(PhoneAuthButtonView(onTap: onTap))
3438
}
3539
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Views/PhoneAuthButtonView.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ import SwiftUI
2020
@MainActor
2121
public struct PhoneAuthButtonView {
2222
@Environment(AuthService.self) private var authService
23+
private let onTap: @MainActor () -> Void
2324

24-
public init() {}
25+
public init(onTap: @escaping @MainActor () -> Void) {
26+
self.onTap = onTap
27+
}
2528
}
2629

2730
extension PhoneAuthButtonView: View {
@@ -31,13 +34,15 @@ extension PhoneAuthButtonView: View {
3134
style: .phone,
3235
accessibilityId: "sign-in-with-phone-button"
3336
) {
34-
authService.navigator.push(.enterPhoneNumber)
37+
onTap()
3538
}
3639
}
3740
}
3841

3942
#Preview {
4043
FirebaseOptions.dummyConfigurationForPreview()
41-
return PhoneAuthButtonView()
42-
.environment(AuthService())
44+
return PhoneAuthButtonView {
45+
print("Phone auth tapped")
46+
}
47+
.environment(AuthService())
4348
}

0 commit comments

Comments
 (0)