Skip to content

Commit 2769e03

Browse files
fix: phone auth is isolated within package
1 parent efbc028 commit 2769e03

5 files changed

Lines changed: 175 additions & 145 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -108,42 +108,6 @@ public final class AuthService {
108108
public var currentMFARequired: MFARequired?
109109
private var currentMFAResolver: MultiFactorResolver?
110110

111-
// MARK: - AuthPickerView Modal APIs
112-
113-
public var isShowingAuthModal = false
114-
115-
public enum AuthModalContentType {
116-
case phoneAuth
117-
}
118-
119-
public var currentModal: AuthModalContentType?
120-
121-
public var authModalViewBuilderRegistry: [AuthModalContentType: () -> AnyView] = [:]
122-
123-
public func registerModalView(for type: AuthModalContentType,
124-
@ViewBuilder builder: @escaping () -> AnyView) {
125-
authModalViewBuilderRegistry[type] = builder
126-
}
127-
128-
public func viewForCurrentModal() -> AnyView? {
129-
guard let type = currentModal,
130-
let builder = authModalViewBuilderRegistry[type] else {
131-
return nil
132-
}
133-
return builder()
134-
}
135-
136-
public func presentModal(for type: AuthModalContentType) {
137-
currentModal = type
138-
isShowingAuthModal = true
139-
}
140-
141-
public func dismissModal() {
142-
isShowingAuthModal = false
143-
}
144-
145-
// MARK: - End AuthPickerView Modal APIs
146-
147111
// MARK: - Provider APIs
148112

149113
private var listenerManager: AuthListenerManager?

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ public struct AuthPickerView {
2626
.authenticationFlow == .signIn ? .signUp : .signIn
2727
}
2828

29-
private var isAuthModalPresented: Binding<Bool> {
30-
Binding(
31-
get: { authService.isShowingAuthModal },
32-
set: { authService.isShowingAuthModal = $0 }
33-
)
34-
}
35-
3629
@ViewBuilder
3730
private var authPickerTitleView: some View {
3831
if authService.authView == .authPicker {
@@ -103,33 +96,6 @@ extension AuthPickerView: View {
10396
EmptyView()
10497
}
10598
}
106-
}.sheet(isPresented: isAuthModalPresented) {
107-
VStack(spacing: 0) {
108-
HStack {
109-
Button(action: {
110-
authService.dismissModal()
111-
}) {
112-
HStack(spacing: 4) {
113-
Image(systemName: "chevron.left")
114-
.font(.system(size: 17, weight: .medium))
115-
Text(authService.string.backButtonLabel)
116-
.font(.system(size: 17))
117-
}
118-
.foregroundColor(.blue)
119-
}
120-
Spacer()
121-
}
122-
.padding()
123-
.background(Color(.systemBackground))
124-
125-
Divider()
126-
127-
if let view = authService.viewForCurrentModal() {
128-
view
129-
.frame(maxWidth: .infinity, maxHeight: .infinity)
130-
.padding()
131-
}
132-
}
13399
}
134100
}
135101
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderAuthUI.swift

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,31 @@ public class PhoneProviderSwift: PhoneAuthProviderSwift {
4141
.credential(withVerificationID: verificationID, verificationCode: verificationCode)
4242
}
4343

44-
// This method is required by the protocol but should not be used for phone auth
45-
// Phone auth requires verification details, so use createPhoneAuthCredential instead
44+
// Present phone auth UI and wait for user to complete the flow
4645
@MainActor public func createAuthCredential() async throws -> AuthCredential {
47-
throw AuthServiceError
48-
.invalidPhoneAuthenticationArguments(
49-
"Phone auth requires verification details. Use createPhoneAuthCredential(verificationID:verificationCode:) instead."
50-
)
46+
guard let presentingViewController = await (UIApplication.shared.connectedScenes
47+
.first as? UIWindowScene)?.windows.first?.rootViewController else {
48+
throw AuthServiceError
49+
.invalidPhoneAuthenticationArguments(
50+
"Root View controller is not available to present Phone auth View."
51+
)
52+
}
53+
54+
return try await withCheckedThrowingContinuation { continuation in
55+
let phoneAuthView = PhoneAuthView(phoneProvider: self) { result in
56+
switch result {
57+
case .success(let credential):
58+
continuation.resume(returning: credential)
59+
case .failure(let error):
60+
continuation.resume(throwing: error)
61+
}
62+
}
63+
64+
let hostingController = UIHostingController(rootView: phoneAuthView)
65+
hostingController.modalPresentationStyle = .formSheet
66+
67+
presentingViewController.present(hostingController, animated: true)
68+
}
5169
}
5270
}
5371

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Views/PhoneAuthButtonView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ public struct PhoneAuthButtonView {
2929
extension PhoneAuthButtonView: View {
3030
public var body: some View {
3131
Button(action: {
32-
authService.registerModalView(for: .phoneAuth) {
33-
AnyView(PhoneAuthView(phoneProvider: phoneProvider).environment(authService))
32+
Task {
33+
try await authService.signIn(phoneProvider)
3434
}
35-
authService.presentModal(for: .phoneAuth)
3635
}) {
3736
Label("Sign in with Phone", systemImage: "phone.fill")
3837
.foregroundColor(.white)

0 commit comments

Comments
 (0)