diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift index c26fed3635..8d1c87c55f 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift @@ -90,6 +90,8 @@ public final class AuthService { private var listenerManager: AuthListenerManager? private var signedInCredential: AuthCredential? + var emailSignInEnabled = false + private var providers: [ExternalAuthProvider] = [] public func register(provider: ExternalAuthProvider) { providers.append(provider) @@ -256,6 +258,11 @@ public extension AuthService { // MARK: - Email/Password Sign In public extension AuthService { + func withEmailSignIn() -> AuthService { + emailSignInEnabled = true + return self + } + func signIn(withEmail email: String, password: String) async throws { let credential = EmailAuthProvider.credential(withEmail: email, password: password) try await signIn(credentials: credential) diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift index 5380ce43ea..cb753567fb 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift @@ -1,13 +1,11 @@ +import FirebaseCore import SwiftUI @MainActor -public struct AuthPickerView { +public struct AuthPickerView { @Environment(AuthService.self) private var authService - let providerButtons: () -> Content - public init(@ViewBuilder providerButtons: @escaping () -> Content) { - self.providerButtons = providerButtons - } + public init() {} private func switchFlow() { authService.authenticationFlow = authService @@ -29,34 +27,43 @@ extension AuthPickerView: View { } else if authService.authView == .emailLink { EmailLinkView() } else { - Text(authService.authenticationFlow == .login ? authService.string - .emailLoginFlowLabel : authService.string.emailSignUpFlowLabel) - VStack { Divider() } - - EmailAuthView() + if authService.emailSignInEnabled { + Text(authService.authenticationFlow == .login ? authService.string + .emailLoginFlowLabel : authService.string.emailSignUpFlowLabel) + Divider() + EmailAuthView() + } VStack { authService.renderButtons() }.padding(.horizontal) - - VStack { Divider() } - HStack { - Text(authService - .authenticationFlow == .login ? authService.string.dontHaveAnAccountYetLabel : - authService.string.alreadyHaveAnAccountLabel) - Button(action: { - withAnimation { - switchFlow() + if authService.emailSignInEnabled { + Divider() + HStack { + Text(authService + .authenticationFlow == .login ? authService.string.dontHaveAnAccountYetLabel : + authService.string.alreadyHaveAnAccountLabel) + Button(action: { + withAnimation { + switchFlow() + } + }) { + Text(authService.authenticationFlow == .signUp ? authService.string + .emailLoginFlowLabel : authService.string.emailSignUpFlowLabel) + .fontWeight(.semibold) + .foregroundColor(.blue) } - }) { - Text(authService.authenticationFlow == .signUp ? authService.string - .emailLoginFlowLabel : authService.string.emailSignUpFlowLabel) - .fontWeight(.semibold) - .foregroundColor(.blue) } + PrivacyTOCsView(displayMode: .footer) + Text(authService.errorMessage).foregroundColor(.red) } - PrivacyTOCsView(displayMode: .footer) - Text(authService.errorMessage).foregroundColor(.red) } } } } + +#Preview { + FirebaseOptions.dummyConfigurationForPreview() + let authService = AuthService() + .withEmailSignIn() + return AuthPickerView().environment(authService) +} diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailAuthView.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailAuthView.swift index 02bb970749..de26519813 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailAuthView.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailAuthView.swift @@ -127,7 +127,7 @@ extension EmailAuthView: View { .frame(maxWidth: .infinity) .buttonStyle(.borderedProminent) Button(action: { - authService.authView = .passwordRecovery + authService.authView = .emailLink }) { Text(authService.string.signUpWithEmailLinkButtonLabel) } diff --git a/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/ContentView.swift b/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/ContentView.swift index b85492a77b..f53772ae5e 100644 --- a/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/ContentView.swift +++ b/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/ContentView.swift @@ -36,11 +36,10 @@ struct ContentView: View { .withGoogleSignIn() .withFacebookSignIn() .withPhoneSignIn() + .withEmailSignIn() } var body: some View { - AuthPickerView { - PhoneAuthButtonView() - }.environment(authService) + AuthPickerView().environment(authService) } }