Branchwell

Jobley / app · jobley.ai / Sources / JobleyUI / PasskeyViews.swift

Raw 3.1 KB on a698b1d1990e3dd0f04a257534b232ac0b1cf6c7
1import SwiftUI
2import JobleyKit
3
4/// Signing in, for the curator and moderators: a passkey, nothing else. A first passkey is added
5/// with the one-time setup link the curator (or, for the curator, the setup endpoint) gives you.
6public struct PasskeySignIn: View {
7 @ObservedObject var curator: CuratorModel
8 @State private var setupLink = ""
9
10 public init(curator: CuratorModel) { self.curator = curator }
11
12 public var body: some View {
13 VStack(alignment: .leading, spacing: 12) {
14 Button {
15 Task { await curator.signInWithPasskey() }
16 } label: {
17 Label(curator.signingIn ? "Waiting for your passkey…" : "Sign in with passkey", systemImage: "person.badge.key")
18 }
19 .buttonStyle(PrimaryButtonStyle())
20 .disabled(curator.signingIn)
21
22 Divider().padding(.vertical, 4)
23 Text("First time? Paste the setup link you were given, then add a passkey on this device.")
24 .font(.callout).foregroundStyle(Theme.ink2).fixedSize(horizontal: false, vertical: true)
25 TextField("Setup link", text: $setupLink, prompt: Text("https://jobley.ai/passkey/#…"))
26 .textFieldStyle(.roundedBorder)
27 .autocorrectionDisabled()
28 #if os(iOS)
29 .textInputAutocapitalization(.never)
30 .keyboardType(.URL)
31 #endif
32 Button("Add a passkey") { Task { if await curator.addFirstPasskey(setupLink: setupLink) { setupLink = "" } } }
33 .disabled(setupLink.isEmpty || curator.signingIn)
34
35 if let error = curator.error { Text(error).font(.caption).foregroundStyle(Theme.danger).fixedSize(horizontal: false, vertical: true) }
36 }
37 }
38}
39
40/// The signed-in person's passkeys: add another (the way to recover) and remove any but the last.
41public struct PasskeyList: View {
42 @ObservedObject var curator: CuratorModel
43
44 public init(curator: CuratorModel) { self.curator = curator }
45
46 public var body: some View {
47 Group {
48 ForEach(curator.passkeys) { passkey in
49 HStack {
50 VStack(alignment: .leading, spacing: 2) {
51 Text(passkey.name ?? "Passkey")
52 Text(passkey.lastUsedAt.map { "Last used \(APIDate.short($0))" } ?? "Added \(APIDate.short(passkey.createdAt))")
53 .font(.caption).foregroundStyle(.secondary)
54 }
55 Spacer()
56 if curator.passkeys.count > 1 {
57 Button("Remove", role: .destructive) { Task { await curator.removePasskey(passkey) } }
58 }
59 }
60 }
61 Button {
62 Task { await curator.addAnotherPasskey() }
63 } label: {
64 Label(curator.signingIn ? "Waiting for your passkey…" : "Add a passkey", systemImage: "plus")
65 }
66 .disabled(curator.signingIn)
67 if let error = curator.error { Text(error).font(.caption).foregroundStyle(Theme.danger) }
68 }
69 .task { await curator.loadPasskeys() }
70 }
71}