Skip to content

Commit 20b9d47

Browse files
chore: move auth flow to authEnvironment + init email provider
1 parent 3ef9459 commit 20b9d47

4 files changed

Lines changed: 69 additions & 10 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/Services/AuthEnvironment.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ public enum AuthenticationState {
1818
case authenticated
1919
}
2020

21+
public enum AuthenticationFlow {
22+
case login
23+
case signUp
24+
}
25+
2126
@MainActor
2227
@Observable
2328
public final class AuthEnvironment {
2429
public static let shared = AuthEnvironment()
2530

2631
var currentUser: User?
32+
var errorMessage = ""
2733

2834
private init() {
2935
setupAuthenticationListener()
@@ -37,6 +43,7 @@ public final class AuthEnvironment {
3743
}
3844

3945
public var authenticationState: AuthenticationState = .unauthenticated
46+
public var authenticationFlow: AuthenticationFlow = .login
4047

4148
private func setupAuthenticationListener() {
4249
authStateHandle = Auth.auth().addStateDidChangeListener { [weak self] _, user in

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/Services/EmailProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import SwiftUI
33

44
@MainActor
5-
class EmailAuthProvider {
5+
public class EmailAuthProvider {
66
@Environment(AuthEnvironment.self) private var authEnvironment
77

88
public init() {}
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import FirebaseAuth
22
import SwiftUI
33

4-
enum AuthenticationFlow {
5-
case login
6-
case signUp
7-
}
8-
4+
@MainActor
95
public struct AuthenticationScreen {
106
@Environment(AuthEnvironment.self) private var authEnvironment
11-
@State private var flow: AuthenticationFlow = .login
127
@State private var errorMessage = ""
138

149
public init() {}
1510

1611
private func switchFlow() {
17-
flow = flow == .login ? .signUp : .login
12+
authEnvironment.authenticationFlow = authEnvironment
13+
.authenticationFlow == .login ? .signUp : .login
1814
errorMessage = ""
1915
}
2016
}
2117

2218
extension AuthenticationScreen: View {
2319
public var body: some View {
2420
VStack {
25-
Text("Authentication screen")
21+
Text(authEnvironment.authenticationFlow == .login ? "Login" : "Sign up")
22+
EmailPasswordView(provider: EmailAuthProvider()).environment(authEnvironment)
2623
}
2724
}
2825
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/Views/EmailPasswordView.swift

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,60 @@
44
//
55
// Created by Russell Wheatley on 20/03/2025.
66
//
7+
import SwiftUI
78

8-
import Foundation
9+
private enum FocusableField: Hashable {
10+
case email
11+
case password
12+
case confirmPassword
13+
}
14+
15+
@MainActor
16+
public struct EmailPasswordView {
17+
@Environment(AuthEnvironment.self) private var authEnvironment
18+
@Environment(\.dismiss) private var dismiss
19+
20+
@State private var provider: EmailAuthProvider
21+
22+
@State private var email = ""
23+
@State private var password = ""
24+
@State private var confirmPassword = ""
25+
26+
@FocusState private var focus: FocusableField?
27+
28+
public init(provider: EmailAuthProvider) {
29+
self.provider = provider
30+
}
31+
32+
private var isValid: Bool {
33+
return if authEnvironment.authenticationFlow == .login {
34+
!email.isEmpty && !password.isEmpty
35+
} else {
36+
!email.isEmpty && !password.isEmpty && password == confirmPassword
37+
}
38+
}
39+
40+
private func signInWithEmailPassword() async {
41+
do {
42+
try await provider.signIn(withEmail: email, password: password)
43+
dismiss()
44+
} catch {
45+
authEnvironment.errorMessage = error.localizedDescription
46+
}
47+
}
48+
49+
private func signUpWithEmailPassword() async {
50+
do {
51+
try await provider.signUp(withEmail: email, password: password)
52+
dismiss()
53+
} catch {
54+
authEnvironment.errorMessage = error.localizedDescription
55+
}
56+
}
57+
}
58+
59+
extension EmailPasswordView: View {
60+
public var body: some View {
61+
Text("EmailPasswordView")
62+
}
63+
}

0 commit comments

Comments
 (0)