-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathEmailAuthView.swift
More file actions
141 lines (127 loc) · 3.66 KB
/
EmailAuthView.swift
File metadata and controls
141 lines (127 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// EmailPasswordView.swift
// FirebaseUI
//
// Created by Russell Wheatley on 20/03/2025.
//
import FirebaseAuth
import FirebaseCore
import SwiftUI
private enum FocusableField: Hashable {
case email
case password
case confirmPassword
}
@MainActor
public struct EmailAuthView {
@Environment(AuthService.self) private var authService
@State private var email = ""
@State private var password = ""
@State private var confirmPassword = ""
@FocusState private var focus: FocusableField?
public init() {}
private var isValid: Bool {
return if authService.authenticationFlow == .login {
!email.isEmpty && !password.isEmpty
} else {
!email.isEmpty && !password.isEmpty && password == confirmPassword
}
}
private func signInWithEmailPassword() async {
do {
try await authService.signIn(withEmail: email, password: password)
} catch {}
}
private func createUserWithEmailPassword() async {
do {
try await authService.createUser(withEmail: email, password: password)
} catch {}
}
}
extension EmailAuthView: View {
public var body: some View {
VStack {
LabeledContent {
TextField("Email", text: $email)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.focused($focus, equals: .email)
.submitLabel(.next)
.onSubmit {
self.focus = .password
}
} label: {
Image(systemName: "at")
}
.padding(.vertical, 6)
.background(Divider(), alignment: .bottom)
.padding(.bottom, 4)
LabeledContent {
SecureField("Password", text: $password)
.focused($focus, equals: .password)
.submitLabel(.go)
.onSubmit {
Task { await signInWithEmailPassword() }
}
} label: {
Image(systemName: "lock")
}
.padding(.vertical, 6)
.background(Divider(), alignment: .bottom)
.padding(.bottom, 8)
if authService.authenticationFlow == .login {
Button(action: {
authService.authView = .passwordRecovery
}) {
Text("Forgotten Password?")
}
}
if authService.authenticationFlow == .signUp {
LabeledContent {
SecureField("Confirm password", text: $confirmPassword)
.focused($focus, equals: .confirmPassword)
.submitLabel(.go)
.onSubmit {
Task { await createUserWithEmailPassword() }
}
} label: {
Image(systemName: "lock")
}
.padding(.vertical, 6)
.background(Divider(), alignment: .bottom)
.padding(.bottom, 8)
}
Button(action: {
Task {
if authService.authenticationFlow == .login { await signInWithEmailPassword() }
else { await createUserWithEmailPassword() }
}
}) {
if authService.authenticationState != .authenticating {
Text(authService.authenticationFlow == .login ? "Log in with password" : "Sign up")
.padding(.vertical, 8)
.frame(maxWidth: .infinity)
} else {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
.padding(.vertical, 8)
.frame(maxWidth: .infinity)
}
}
.disabled(!isValid)
.padding([.top, .bottom], 8)
.frame(maxWidth: .infinity)
.buttonStyle(.borderedProminent)
Button(action: {
authService.authView = .passwordRecovery
}) {
Text("Prefer Email link sign-in?")
}
}
}
}
#Preview {
FirebaseOptions.dummyConfigurationForPreview()
return EmailAuthView()
.environment(AuthService())
}