Skip to content

refactor: email sign-in opt-in, remove providerButton closure #1252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import FirebaseCore
import SwiftUI

@MainActor
public struct AuthPickerView<Content: View> {
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
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ extension EmailAuthView: View {
.frame(maxWidth: .infinity)
.buttonStyle(.borderedProminent)
Button(action: {
authService.authView = .passwordRecovery
authService.authView = .emailLink
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be two separate buttons?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morganchen12 Also - I have a question in the description regarding whether calling AuthService() .withEmailSignIn() should also hide email sign-in. I'm 99% sure that it should but just wanted confirmation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the opposite, where not calling .withEmailSignIn() should hide it? That sounds good to me.

}) {
Text(authService.string.signUpWithEmailLinkButtonLabel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ struct ContentView: View {
.withGoogleSignIn()
.withFacebookSignIn()
.withPhoneSignIn()
.withEmailSignIn()
}

var body: some View {
AuthPickerView {
PhoneAuthButtonView()
}.environment(authService)
AuthPickerView().environment(authService)
}
}