Skip to content

Commit dea04f6

Browse files
refactor: make phone auth a sheet that presents over AuthPickerView
1 parent 0410fb8 commit dea04f6

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public enum AuthView {
3434
case authPicker
3535
case passwordRecovery
3636
case emailLink
37-
case phoneAuth
3837
case updatePassword
3938
}
4039

@@ -84,6 +83,44 @@ public final class AuthService {
8483
public var authenticationFlow: AuthenticationFlow = .login
8584
public var errorMessage = ""
8685
public let passwordPrompt: PasswordPromptCoordinator = .init()
86+
87+
// MARK: - AuthPickerView Modal APIs
88+
89+
public var isShowingAuthModal = false
90+
91+
public enum AuthModalContentType {
92+
case phoneAuth
93+
}
94+
95+
public var currentModal: AuthModalContentType?
96+
97+
public var authModalViewBuilderRegistry: [AuthModalContentType: () -> AnyView] = [:]
98+
99+
public func registerModalView(for type: AuthModalContentType, builder: @escaping () -> AnyView) {
100+
authModalViewBuilderRegistry[type] = builder
101+
}
102+
103+
public func viewForCurrentModal() -> AnyView? {
104+
guard let type = currentModal,
105+
let builder = authModalViewBuilderRegistry[type] else {
106+
return nil
107+
}
108+
return builder()
109+
}
110+
111+
public func presentModal(for type: AuthModalContentType) {
112+
currentModal = type
113+
isShowingAuthModal = true
114+
}
115+
116+
public func dismissModal() {
117+
isShowingAuthModal = false
118+
}
119+
120+
// MARK: - End AuthPickerView Modal APIs
121+
122+
// MARK: - Provider APIs
123+
87124
private var unsafeGoogleProvider: (any GoogleProviderAuthUIProtocol)?
88125
private var unsafeFacebookProvider: (any FacebookProviderAuthUIProtocol)?
89126
private var unsafePhoneAuthProvider: (any PhoneAuthProviderAuthUIProtocol)?
@@ -147,6 +184,8 @@ public final class AuthService {
147184
}
148185
}
149186

187+
// MARK: - End Provider APIs
188+
150189
private func safeActionCodeSettings() throws -> ActionCodeSettings {
151190
// email sign-in requires action code settings
152191
guard let actionCodeSettings = configuration

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ public struct AuthPickerView {
1111
authService.authenticationFlow = authService
1212
.authenticationFlow == .login ? .signUp : .login
1313
}
14+
15+
private var isAuthModalPresented: Binding<Bool> {
16+
Binding(
17+
get: { authService.isShowingAuthModal },
18+
set: { authService.isShowingAuthModal = $0 }
19+
)
20+
}
1421
}
1522

1623
extension AuthPickerView: View {
@@ -28,8 +35,6 @@ extension AuthPickerView: View {
2835
} else if authService.authView == .emailLink {
2936
Divider()
3037
EmailLinkView()
31-
} else if authService.authView == .phoneAuth {
32-
// TODO: - how are we rendering the phone auth View??
3338
} else {
3439
Divider()
3540
if authService.emailSignInEnabled {
@@ -64,6 +69,33 @@ extension AuthPickerView: View {
6469
PrivacyTOCsView(displayMode: .footer)
6570
Text(authService.errorMessage).foregroundColor(.red)
6671
}
72+
}.sheet(isPresented: isAuthModalPresented) {
73+
VStack(spacing: 0) {
74+
HStack {
75+
Button(action: {
76+
authService.dismissModal()
77+
}) {
78+
HStack(spacing: 4) {
79+
Image(systemName: "chevron.left")
80+
.font(.system(size: 17, weight: .medium))
81+
Text(authService.string.backButtonLabel)
82+
.font(.system(size: 17))
83+
}
84+
.foregroundColor(.blue)
85+
}
86+
Spacer()
87+
}
88+
.padding()
89+
.background(Color(.systemBackground))
90+
91+
Divider()
92+
93+
if let view = authService.viewForCurrentModal() {
94+
view
95+
.frame(maxWidth: .infinity, maxHeight: .infinity)
96+
.padding()
97+
}
98+
}
6799
}
68100
}
69101
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/PasswordRecoveryView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension PasswordRecoveryView: View {
6969
}
7070
.padding()
7171
}.onOpenURL { _ in
72-
// move the user to email/password View
72+
// TODO: - move the user to email/password View
7373
}
7474
.navigationBarItems(leading: Button(action: {
7575
authService.authView = .authPicker

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Views/PhoneAuthButtonView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ public struct PhoneAuthButtonView {
1212
extension PhoneAuthButtonView: View {
1313
public var body: some View {
1414
Button(action: {
15-
authService.authView = .phoneAuth
15+
authService.registerModalView(for: .phoneAuth) {
16+
AnyView(PhoneAuthView().environment(authService))
17+
}
18+
authService.presentModal(for: .phoneAuth)
1619
}) {
1720
Label("Sign in with Phone", systemImage: "phone.fill")
1821
.foregroundColor(.white)

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Views/PhoneAuthView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ extension PhoneAuthView: View {
9595
.padding(.vertical, 8)
9696
.frame(maxWidth: .infinity)
9797
}
98-
Text(errorMessage).foregroundColor(.red)
9998
}
10099
}
101100

0 commit comments

Comments
 (0)