Add minimal sign-in page

This commit is contained in:
lucast committed 2026-10-07 01:05:17 +02:00
1 parent 36f2b2d843
commit 13d8597eea
4 files changed
+223 -10

No files matched your search

+3
View File
@@ -37,6 +37,9 @@ The iOS app should feel native to iOS and use an Apple Music-inspired **liquid g
## Working principles ## Working principles
- The user directs product decisions; keep implementation clear and maintainable, with comments where they clarify non-obvious behavior. - The user directs product decisions; keep implementation clear and maintainable, with comments where they clarify non-obvious behavior.
- Before creating or updating a UI, inspect the corresponding screen and shared layout in `~/music-v2`. Mirror its structure and information hierarchy in native SwiftUI, using liquid glass only where it suits the screen and supports clarity.
- Use Apple's standard red accent (`.red`) and otherwise prefer SwiftUI's semantic system colors and materials. Avoid custom color values unless the user asks for them.
- Always add a SwiftUI preview for every UI view you create or update.
- Before completing code changes, run available linting and type checking or a build for the affected code. Fix errors introduced by the change, and clearly report any unrelated baseline or environment failures that prevent a clean check. - Before completing code changes, run available linting and type checking or a build for the affected code. Fix errors introduced by the change, and clearly report any unrelated baseline or environment failures that prevent a clean check.
- Reuse the web app’s backend API and data model. Inspect `~/music-v2` when route behavior, authentication, content fields, or feature scope is unclear. - Reuse the web app’s backend API and data model. Inspect `~/music-v2` when route behavior, authentication, content fields, or feature scope is unclear.
- Match the web version’s structure and scope, while making the experience idiomatic to SwiftUI and iOS. - Match the web version’s structure and scope, while making the experience idiomatic to SwiftUI and iOS.
+33
View File
@@ -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
}
}
+164
View File
@@ -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())
}
}
+23 -10
View File
@@ -1,16 +1,22 @@
//
// ContentView.swift
// Music
//
// Created by Lucas Täkker on 06/10/2026.
//
import SwiftUI import SwiftUI
struct ContentView: View { struct ContentView: View {
@StateObject private var session = AuthSessionStore()
var body: some View { var body: some View {
VStack { Group {
Image(systemName: "globe") switch session.state {
case .checking:
ProgressView()
.tint(.red)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(uiColor: .systemBackground))
.accessibilityLabel("Checking sign-in")
case .signedOut:
SignInView(session: session)
case .signedIn:
VStack(spacing: 12) {
Image(systemName: "music.note.list")
.imageScale(.large) .imageScale(.large)
.foregroundStyle(.tint) .foregroundStyle(.tint)
Text("Hello, world!") Text("Hello, world!")
@@ -18,7 +24,14 @@ struct ContentView: View {
.padding() .padding()
} }
} }
.task {
await session.restoreSession()
}
}
}
#Preview { struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView() ContentView()
} }
}