Skip to content

Commit 09c8c00

Browse files
chore: update email auth logic
1 parent 8278f04 commit 09c8c00

5 files changed

Lines changed: 60 additions & 24 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ public final class AuthEnvironment {
8383
}
8484

8585
func signIn(with credentials: AuthCredential) async throws {
86-
try await auth.signIn(with: credentials)
87-
updateAuthenticationState()
86+
authenticationState = .authenticating
87+
do {
88+
try await auth.signIn(with: credentials)
89+
updateAuthenticationState()
90+
} catch {
91+
authenticationState = .unauthenticated
92+
throw error
93+
}
8894
}
8995
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ public class EmailAuthProvider {
1212
func signIn(withEmail email: String, password: String) async throws {
1313
authEnvironment.authenticationState = .authenticating
1414
do {
15-
try await authEnvironment.auth.createUser(withEmail: email, password: password)
15+
try await authEnvironment.auth.signIn(withEmail: email, password: password)
16+
authEnvironment.updateAuthenticationState()
1617
} catch {
1718
authEnvironment.authenticationState = .unauthenticated
1819
throw error
1920
}
2021
}
2122

22-
func signUp(withEmail email: String, password: String) async throws {
23+
func createUser(withEmail email: String, password: String) async throws {
2324
authEnvironment.authenticationState = .authenticating
2425
do {
2526
try await authEnvironment.auth.createUser(withEmail: email, password: password)
27+
authEnvironment.updateAuthenticationState()
2628
} catch {
2729
authEnvironment.authenticationState = .unauthenticated
2830
throw error

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/FirebaseAuthSwiftUI/Views/AuthenticationScreen.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,41 @@ import SwiftUI
44
@MainActor
55
public struct AuthenticationScreen {
66
@Environment(AuthEnvironment.self) private var authEnvironment
7-
@State private var errorMessage = ""
87

98
public init() {}
109

1110
private func switchFlow() {
1211
authEnvironment.authenticationFlow = authEnvironment
1312
.authenticationFlow == .login ? .signUp : .login
14-
errorMessage = ""
13+
authEnvironment.errorMessage = ""
1514
}
1615
}
1716

1817
extension AuthenticationScreen: View {
1918
public var body: some View {
2019
VStack {
21-
Text(authEnvironment.authenticationFlow == .login ? "Login" : "Sign up")
22-
EmailPasswordView(provider: EmailAuthProvider(authEnvironment: authEnvironment))
23-
HStack {
24-
Text(authEnvironment
25-
.authenticationFlow == .login ? "Don't have an account yet?" : "Already have an account?")
26-
Button(action: {
27-
withAnimation {
28-
switchFlow()
20+
if authEnvironment.authenticationState == .authenticated {
21+
SignedInView()
22+
} else {
23+
Text(authEnvironment.authenticationFlow == .login ? "Login" : "Sign up")
24+
EmailPasswordView(provider: EmailAuthProvider(authEnvironment: authEnvironment))
25+
HStack {
26+
Text(authEnvironment
27+
.authenticationFlow == .login ? "Don't have an account yet?" :
28+
"Already have an account?")
29+
Button(action: {
30+
withAnimation {
31+
switchFlow()
32+
}
33+
}) {
34+
Text(authEnvironment.authenticationFlow == .signUp ? "Log in" : "Sign up")
35+
.fontWeight(.semibold)
36+
.foregroundColor(.blue)
2937
}
30-
}) {
31-
Text(authEnvironment.authenticationFlow == .signUp ? "Log in" : "Sign up")
32-
.fontWeight(.semibold)
33-
.foregroundColor(.blue)
3438
}
3539
}
40+
Text(authEnvironment.errorMessage)
41+
.foregroundColor(.red)
3642
}
3743
}
3844
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public struct EmailPasswordView {
4646
}
4747
}
4848

49-
private func signUpWithEmailPassword() async {
49+
private func createUserWithEmailPassword() async {
5050
do {
51-
try await provider.signUp(withEmail: email, password: password)
51+
try await provider.createUser(withEmail: email, password: password)
5252
dismiss()
5353
} catch {
5454
authEnvironment.errorMessage = error.localizedDescription
@@ -90,8 +90,10 @@ extension EmailPasswordView: View {
9090
.padding(.bottom, 8)
9191

9292
if authEnvironment.authenticationFlow == .login {
93-
Button("Forgot password?") {}
94-
.frame(maxWidth: .infinity, alignment: .trailing)
93+
Button("Forgot password?") {
94+
// TODO: - does this update to a new screen or update in-situ???
95+
}
96+
.frame(maxWidth: .infinity, alignment: .trailing)
9597
}
9698

9799
if authEnvironment.authenticationFlow == .signUp {
@@ -100,7 +102,7 @@ extension EmailPasswordView: View {
100102
.focused($focus, equals: .confirmPassword)
101103
.submitLabel(.go)
102104
.onSubmit {
103-
Task { await signUpWithEmailPassword() }
105+
Task { await createUserWithEmailPassword() }
104106
}
105107
} label: {
106108
Image(systemName: "lock")
@@ -113,7 +115,7 @@ extension EmailPasswordView: View {
113115
Button(action: {
114116
Task {
115117
if authEnvironment.authenticationFlow == .login { await signInWithEmailPassword() }
116-
else { await signUpWithEmailPassword() }
118+
else { await createUserWithEmailPassword() }
117119
}
118120
}) {
119121
if authEnvironment.authenticationState != .authenticating {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import SwiftUI
2+
3+
@MainActor
4+
public struct SignedInView {
5+
@Environment(AuthEnvironment.self) private var authEnvironment
6+
}
7+
8+
extension SignedInView: View {
9+
public var body: some View {
10+
VStack {
11+
Text("Signed in")
12+
Text("User: \(authEnvironment.currentUser?.email ?? "Unknown")")
13+
Button("Sign out") {
14+
Task {
15+
try? await authEnvironment.signOut()
16+
}
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)