Skip to content

Commit cb72740

Browse files
refactor: email sign-in opt-in, remove providerButton closure
1 parent ef5d86c commit cb72740

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public final class AuthService {
9090
private var listenerManager: AuthListenerManager?
9191
private var signedInCredential: AuthCredential?
9292

93+
var emailSignInEnabled = false
94+
9395
private var providers: [ExternalAuthProvider] = []
9496
public func register(provider: ExternalAuthProvider) {
9597
providers.append(provider)
@@ -256,6 +258,11 @@ public extension AuthService {
256258
// MARK: - Email/Password Sign In
257259

258260
public extension AuthService {
261+
func withEmailSignIn() -> AuthService {
262+
emailSignInEnabled = true
263+
return self
264+
}
265+
259266
func signIn(withEmail email: String, password: String) async throws {
260267
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
261268
try await signIn(credentials: credential)

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Utils/StringUtils.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,14 @@ public class StringUtils {
308308
public var enterPhoneNumberLabel: String {
309309
return localizedString(for: "Enter phone number")
310310
}
311+
311312
/// Phone provider
312313
/// found in:
313314
/// - PhoneAuthButtonView
314315
public var phoneNumberVerificationCodeLabel: String {
315316
return localizedString(for: "Enter verification code")
316317
}
318+
317319
/// Phone provider
318320
/// found in:
319321
/// - PhoneAuthButtonView

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import SwiftUI
22

33
@MainActor
4-
public struct AuthPickerView<Content: View> {
4+
public struct AuthPickerView {
55
@Environment(AuthService.self) private var authService
6-
let providerButtons: () -> Content
76

8-
public init(@ViewBuilder providerButtons: @escaping () -> Content) {
9-
self.providerButtons = providerButtons
10-
}
7+
public init() {}
118

129
private func switchFlow() {
1310
authService.authenticationFlow = authService
@@ -29,28 +26,32 @@ extension AuthPickerView: View {
2926
} else if authService.authView == .emailLink {
3027
EmailLinkView()
3128
} else {
32-
Text(authService.authenticationFlow == .login ? authService.string
33-
.emailLoginFlowLabel : authService.string.emailSignUpFlowLabel)
34-
VStack { Divider() }
35-
EmailAuthView()
29+
if authService.emailSignInEnabled {
30+
Text(authService.authenticationFlow == .login ? authService.string
31+
.emailLoginFlowLabel : authService.string.emailSignUpFlowLabel)
32+
VStack { Divider() }
33+
EmailAuthView()
34+
}
3635
authService.renderButtons()
37-
VStack { Divider() }
38-
HStack {
39-
Text(authService
40-
.authenticationFlow == .login ? authService.string.dontHaveAnAccountYetLabel :
41-
authService.string.alreadyHaveAnAccountLabel)
42-
Button(action: {
43-
withAnimation {
44-
switchFlow()
36+
if authService.emailSignInEnabled {
37+
VStack { Divider() }
38+
HStack {
39+
Text(authService
40+
.authenticationFlow == .login ? authService.string.dontHaveAnAccountYetLabel :
41+
authService.string.alreadyHaveAnAccountLabel)
42+
Button(action: {
43+
withAnimation {
44+
switchFlow()
45+
}
46+
}) {
47+
Text(authService.authenticationFlow == .signUp ? authService.string
48+
.emailLoginFlowLabel : authService.string.emailSignUpFlowLabel)
49+
.fontWeight(.semibold)
50+
.foregroundColor(.blue)
4551
}
46-
}) {
47-
Text(authService.authenticationFlow == .signUp ? authService.string
48-
.emailLoginFlowLabel : authService.string.emailSignUpFlowLabel)
49-
.fontWeight(.semibold)
50-
.foregroundColor(.blue)
5152
}
53+
Text(authService.errorMessage).foregroundColor(.red)
5254
}
53-
Text(authService.errorMessage).foregroundColor(.red)
5455
}
5556
}
5657
}

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/ContentView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ struct ContentView: View {
3434
.withGoogleSignIn()
3535
.withFacebookSignIn()
3636
.withPhoneSignIn()
37+
.withEmailSignIn()
3738
}
3839

3940
var body: some View {
40-
AuthPickerView {
41-
PhoneAuthButtonView()
42-
}.environment(authService)
41+
AuthPickerView().environment(authService)
4342
}
4443
}

0 commit comments

Comments
 (0)