Skip to content

Commit 3eb4436

Browse files
refactor: move closer to preliminary design
1 parent d337532 commit 3eb4436

12 files changed

Lines changed: 137 additions & 221 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/FUIAuth.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/FUIAuthProvider.swift

Lines changed: 0 additions & 26 deletions
This file was deleted.

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/FUIAuthState.swift

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
@preconcurrency import FirebaseAuth
2+
import SwiftUI
3+
4+
public enum AuthenticationProvider {
5+
case email
6+
case google
7+
}
8+
9+
public enum AuthenticationOperationType: String {
10+
case signIn
11+
case signUp
12+
case deleteAccount
13+
}
14+
15+
public enum AuthenticationState {
16+
case unauthenticated
17+
case authenticating
18+
case authenticated
19+
}
20+
21+
@MainActor
22+
@Observable
23+
public final class AuthEnvironment {
24+
public static let shared = AuthEnvironment()
25+
26+
var currentUser: User?
27+
28+
private init() {
29+
setupAuthenticationListener()
30+
}
31+
32+
deinit {
33+
if let handle = authStateHandle {
34+
Auth.auth().removeStateDidChangeListener(handle)
35+
authStateHandle = nil
36+
}
37+
}
38+
39+
public var authenticationState: AuthenticationState = .unauthenticated
40+
41+
private func setupAuthenticationListener() {
42+
authStateHandle = Auth.auth().addStateDidChangeListener { [weak self] _, user in
43+
self?.currentUser = user
44+
self?.updateAuthenticationState()
45+
}
46+
}
47+
48+
private nonisolated(unsafe) var authStateHandle: AuthStateDidChangeListenerHandle? {
49+
willSet {
50+
if let handle = authStateHandle {
51+
Auth.auth().removeStateDidChangeListener(handle)
52+
}
53+
}
54+
}
55+
56+
func updateAuthenticationState() {
57+
authenticationState =
58+
(currentUser == nil || currentUser?.isAnonymous == true)
59+
? .unauthenticated
60+
: .authenticated
61+
}
62+
63+
public func signOut() throws {
64+
try Auth.auth().signOut()
65+
}
66+
67+
func signIn(with credentials: AuthCredential) async throws {
68+
try await Auth.auth().signIn(with: credentials)
69+
updateAuthenticationState()
70+
}
71+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@preconcurrency import FirebaseAuth
2+
import SwiftUI
3+
4+
@MainActor
5+
class EmailAuthProvider {
6+
@Environment(AuthEnvironment.self) private var authEnvironment
7+
8+
public init() {}
9+
10+
func signIn(withEmail email: String, password: String) async throws {
11+
authEnvironment.authenticationState = .authenticating
12+
do {
13+
try await Auth.auth().createUser(withEmail: email, password: password)
14+
} catch {
15+
authEnvironment.authenticationState = .unauthenticated
16+
throw error
17+
}
18+
}
19+
20+
func signUp(withEmail email: String, password: String) async throws {
21+
authEnvironment.authenticationState = .authenticating
22+
do {
23+
try await Auth.auth().createUser(withEmail: email, password: password)
24+
} catch {
25+
authEnvironment.authenticationState = .unauthenticated
26+
throw error
27+
}
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import FirebaseAuth
2+
import SwiftUI
3+
4+
enum AuthenticationFlow {
5+
case login
6+
case signUp
7+
}
8+
9+
public struct AuthenticationScreen {
10+
@Environment(AuthEnvironment.self) private var authEnvironment
11+
12+
@State private var flow: AuthenticationFlow = .login
13+
14+
private func switchFlow() {
15+
flow = flow == .login ? .signUp : .login
16+
errorMessage = ""
17+
}
18+
19+
@State private var errorMessage = ""
20+
}
21+
22+
extension AuthenticationScreen: View {
23+
public var body: some View {
24+
VStack {
25+
Text("Authentication screen")
26+
}
27+
}
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// EmailPasswordView.swift
3+
// FirebaseUI
4+
//
5+
// Created by Russell Wheatley on 20/03/2025.
6+
//
7+
8+
import Foundation

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/Views/FUIAuthPicker.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/Views/FUIAuthView.swift

Lines changed: 0 additions & 26 deletions
This file was deleted.

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/Views/Warning.swift

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)