Add minimal sign-in page
This commit is contained in:
1 parent
36f2b2d843
commit
13d8597eea
4 files changed
+228
-15
No files matched your search
@@ -0,0 +1,33 @@
|
||||
import Combine
|
||||
|
||||
@MainActor
|
||||
final class AuthSessionStore: ObservableObject {
|
||||
enum State {
|
||||
case checking
|
||||
case signedOut
|
||||
case signedIn
|
||||
}
|
||||
|
||||
@Published private(set) var state: State = .checking
|
||||
@Published private(set) var isSigningIn = false
|
||||
|
||||
private let api = MusicAPI()
|
||||
|
||||
func restoreSession() async {
|
||||
guard state == .checking else { return }
|
||||
|
||||
do {
|
||||
state = try await api.sessionStatus().authenticated ? .signedIn : .signedOut
|
||||
} catch {
|
||||
state = .signedOut
|
||||
}
|
||||
}
|
||||
|
||||
func signIn(email: String, password: String) async throws {
|
||||
isSigningIn = true
|
||||
defer { isSigningIn = false }
|
||||
|
||||
try await api.signIn(email: email, password: password)
|
||||
state = .signedIn
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
import SwiftUI
|
||||
|
||||
struct SignInView: View {
|
||||
@ObservedObject var session: AuthSessionStore
|
||||
|
||||
@State private var email = ""
|
||||
@State private var password = ""
|
||||
@State private var errorMessage: String?
|
||||
@FocusState private var focusedField: Field?
|
||||
|
||||
private enum Field: Hashable {
|
||||
case email
|
||||
case password
|
||||
}
|
||||
|
||||
private var canSubmit: Bool {
|
||||
!email.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
&& !password.isEmpty
|
||||
&& !session.isSigningIn
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
ScrollView {
|
||||
formCard
|
||||
.frame(maxWidth: 440)
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.horizontal, 24)
|
||||
.padding(.vertical, 24)
|
||||
.frame(minHeight: geometry.size.height)
|
||||
}
|
||||
.scrollIndicators(.hidden)
|
||||
.scrollDismissesKeyboard(.interactively)
|
||||
}
|
||||
.background(Color(uiColor: .systemBackground).ignoresSafeArea())
|
||||
}
|
||||
|
||||
private var formCard: some View {
|
||||
VStack(spacing: 18) {
|
||||
Text("Welcome back!")
|
||||
.font(.system(size: 24, weight: .semibold))
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
|
||||
VStack(spacing: 13) {
|
||||
emailField
|
||||
passwordField
|
||||
}
|
||||
|
||||
if let errorMessage {
|
||||
Label(errorMessage, systemImage: "exclamationmark.circle.fill")
|
||||
.font(.footnote)
|
||||
.foregroundStyle(.red.opacity(0.9))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
}
|
||||
|
||||
Button(action: submit) {
|
||||
HStack(spacing: 9) {
|
||||
if session.isSigningIn {
|
||||
ProgressView()
|
||||
.tint(.white)
|
||||
}
|
||||
Text(session.isSigningIn ? "Signing in…" : "Sign in")
|
||||
.font(.headline)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(minHeight: 48)
|
||||
.foregroundStyle(.white)
|
||||
.background(.red, in: RoundedRectangle(cornerRadius: 14))
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.disabled(!canSubmit)
|
||||
.opacity(canSubmit ? 1 : 0.55)
|
||||
.accessibilityHint("Signs in with your email address and password")
|
||||
|
||||
Text("Sorry, but registrations are closed")
|
||||
.font(.footnote)
|
||||
.foregroundStyle(.secondary)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
.padding(22)
|
||||
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 22))
|
||||
.overlay {
|
||||
RoundedRectangle(cornerRadius: 22)
|
||||
.strokeBorder(.primary.opacity(0.08), lineWidth: 1)
|
||||
}
|
||||
}
|
||||
|
||||
private var emailField: some View {
|
||||
VStack(alignment: .leading, spacing: 7) {
|
||||
Text("Email address")
|
||||
.font(.footnote.weight(.medium))
|
||||
.foregroundStyle(.secondary)
|
||||
|
||||
TextField("E-mail", text: $email)
|
||||
.keyboardType(.emailAddress)
|
||||
.textContentType(.emailAddress)
|
||||
.textInputAutocapitalization(.never)
|
||||
.autocorrectionDisabled()
|
||||
.submitLabel(.next)
|
||||
.focused($focusedField, equals: .email)
|
||||
.onSubmit { focusedField = .password }
|
||||
.padding(.horizontal, 13)
|
||||
.frame(minHeight: 48)
|
||||
.background(
|
||||
Color(uiColor: .secondarySystemBackground),
|
||||
in: RoundedRectangle(cornerRadius: 12)
|
||||
)
|
||||
.overlay {
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.strokeBorder(.primary.opacity(0.08), lineWidth: 1)
|
||||
}
|
||||
.accessibilityLabel("Email address")
|
||||
}
|
||||
}
|
||||
|
||||
private var passwordField: some View {
|
||||
VStack(alignment: .leading, spacing: 7) {
|
||||
Text("Password")
|
||||
.font(.footnote.weight(.medium))
|
||||
.foregroundStyle(.secondary)
|
||||
|
||||
SecureField("Password", text: $password)
|
||||
.textContentType(.password)
|
||||
.submitLabel(.go)
|
||||
.focused($focusedField, equals: .password)
|
||||
.onSubmit(submit)
|
||||
.padding(.horizontal, 13)
|
||||
.frame(minHeight: 48)
|
||||
.background(
|
||||
Color(uiColor: .secondarySystemBackground),
|
||||
in: RoundedRectangle(cornerRadius: 12)
|
||||
)
|
||||
.overlay {
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.strokeBorder(.primary.opacity(0.08), lineWidth: 1)
|
||||
}
|
||||
.accessibilityLabel("Password")
|
||||
}
|
||||
}
|
||||
|
||||
private func submit() {
|
||||
guard canSubmit else { return }
|
||||
focusedField = nil
|
||||
errorMessage = nil
|
||||
|
||||
Task {
|
||||
do {
|
||||
try await session.signIn(
|
||||
email: email.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||
password: password
|
||||
)
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SignInView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
SignInView(session: AuthSessionStore())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user