Skip to content

Commit 0a5962f

Browse files
Merge pull request #1252 from firebase/opt-in-email-provider
2 parents f557bdb + 10bdd3e commit 0a5962f

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
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
switch provider {
@@ -296,6 +298,11 @@ public extension AuthService {
296298
// MARK: - Email/Password Sign In
297299

298300
public extension AuthService {
301+
func withEmailSignIn() -> AuthService {
302+
emailSignInEnabled = true
303+
return self
304+
}
305+
299306
func signIn(withEmail email: String, password: String) async throws {
300307
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
301308
try await signIn(credentials: credential)
Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
import FirebaseCore
12
import SwiftUI
23

34
@MainActor
4-
public struct AuthPickerView<Content: View> {
5+
public struct AuthPickerView {
56
@Environment(AuthService.self) private var authService
6-
let providerButtons: () -> Content
77

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

1210
private func switchFlow() {
1311
authService.authenticationFlow = authService
@@ -29,34 +27,43 @@ extension AuthPickerView: View {
2927
} else if authService.authView == .emailLink {
3028
EmailLinkView()
3129
} else {
32-
Text(authService.authenticationFlow == .login ? authService.string
33-
.emailLoginFlowLabel : authService.string.emailSignUpFlowLabel)
34-
VStack { Divider() }
35-
36-
EmailAuthView()
30+
if authService.emailSignInEnabled {
31+
Text(authService.authenticationFlow == .login ? authService.string
32+
.emailLoginFlowLabel : authService.string.emailSignUpFlowLabel)
33+
Divider()
34+
EmailAuthView()
35+
}
3736
VStack {
3837
authService.renderButtons()
3938
}.padding(.horizontal)
40-
41-
VStack { Divider() }
42-
HStack {
43-
Text(authService
44-
.authenticationFlow == .login ? authService.string.dontHaveAnAccountYetLabel :
45-
authService.string.alreadyHaveAnAccountLabel)
46-
Button(action: {
47-
withAnimation {
48-
switchFlow()
39+
if authService.emailSignInEnabled {
40+
Divider()
41+
HStack {
42+
Text(authService
43+
.authenticationFlow == .login ? authService.string.dontHaveAnAccountYetLabel :
44+
authService.string.alreadyHaveAnAccountLabel)
45+
Button(action: {
46+
withAnimation {
47+
switchFlow()
48+
}
49+
}) {
50+
Text(authService.authenticationFlow == .signUp ? authService.string
51+
.emailLoginFlowLabel : authService.string.emailSignUpFlowLabel)
52+
.fontWeight(.semibold)
53+
.foregroundColor(.blue)
4954
}
50-
}) {
51-
Text(authService.authenticationFlow == .signUp ? authService.string
52-
.emailLoginFlowLabel : authService.string.emailSignUpFlowLabel)
53-
.fontWeight(.semibold)
54-
.foregroundColor(.blue)
5555
}
56+
PrivacyTOCsView(displayMode: .footer)
57+
Text(authService.errorMessage).foregroundColor(.red)
5658
}
57-
PrivacyTOCsView(displayMode: .footer)
58-
Text(authService.errorMessage).foregroundColor(.red)
5959
}
6060
}
6161
}
6262
}
63+
64+
#Preview {
65+
FirebaseOptions.dummyConfigurationForPreview()
66+
let authService = AuthService()
67+
.withEmailSignIn()
68+
return AuthPickerView().environment(authService)
69+
}

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/ContentView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ struct ContentView: View {
3636
.withGoogleSignIn()
3737
.withFacebookSignIn()
3838
.withPhoneSignIn()
39+
.withEmailSignIn()
3940
}
4041

4142
var body: some View {
42-
AuthPickerView {
43-
PhoneAuthButtonView()
44-
}.environment(authService)
43+
AuthPickerView().environment(authService)
4544
}
4645
}

0 commit comments

Comments
 (0)