Skip to content

Commit a4e5835

Browse files
Merge branch 'refactor-remaining-packages' into apple-provider
2 parents ba182b7 + 2424b6c commit a4e5835

File tree

5 files changed

+169
-154
lines changed

5 files changed

+169
-154
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -109,42 +109,6 @@ public final class AuthService {
109109
private var currentMFAResolver: MultiFactorResolver?
110110
private var pendingMFACredential: AuthCredential?
111111

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

150114
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: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,34 @@ public class PhoneProviderSwift: PhoneAuthProviderSwift {
3434
}
3535
}
3636

37-
// Create a phone auth credential with the verification details
38-
public func createPhoneAuthCredential(verificationID: String,
39-
verificationCode: String) -> AuthCredential {
40-
return PhoneAuthProvider.provider()
41-
.credential(withVerificationID: verificationID, verificationCode: verificationCode)
42-
}
43-
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
37+
// Present phone auth UI and wait for user to complete the flow
4638
@MainActor public func createAuthCredential() async throws -> AuthCredential {
47-
throw AuthServiceError
48-
.invalidPhoneAuthenticationArguments(
49-
"Phone auth requires verification details. Use createPhoneAuthCredential(verificationID:verificationCode:) instead."
50-
)
39+
guard let presentingViewController = await (UIApplication.shared.connectedScenes
40+
.first as? UIWindowScene)?.windows.first?.rootViewController else {
41+
throw AuthServiceError
42+
.invalidPhoneAuthenticationArguments(
43+
"Root View controller is not available to present Phone auth View."
44+
)
45+
}
46+
47+
return try await withCheckedThrowingContinuation { continuation in
48+
let phoneAuthView = PhoneAuthView(phoneProvider: self) { result in
49+
switch result {
50+
case let .success(verificationID, verificationCode):
51+
// Create the credential here
52+
let credential = PhoneAuthProvider.provider()
53+
.credential(withVerificationID: verificationID, verificationCode: verificationCode)
54+
continuation.resume(returning: credential)
55+
case let .failure(error):
56+
continuation.resume(throwing: error)
57+
}
58+
}
59+
60+
let hostingController = UIHostingController(rootView: phoneAuthView)
61+
hostingController.modalPresentationStyle = .formSheet
62+
63+
presentingViewController.present(hostingController, animated: true)
64+
}
5165
}
5266
}
5367

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)