-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSetupForm.tsx
More file actions
193 lines (173 loc) · 6.31 KB
/
Copy pathSetupForm.tsx
File metadata and controls
193 lines (173 loc) · 6.31 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// src/app/setup/SetupForm.tsx
"use client"
import { H2 } from "@typography"
import Image from "next/image"
import { useRouter } from "next/navigation"
import { type SubmitEvent, useState } from "react"
import { Button, Card, Input, Toggle } from "@/components/ui"
import { Notice } from "@/components/ui/Notice"
import { SNAPSHOT_RETENTION_MAX, SNAPSHOT_RETENTION_MIN } from "@/lib/limits"
export function SetupForm() {
const router = useRouter()
const [username, setUsername] = useState("")
const [password, setPassword] = useState("")
const [confirmPassword, setConfirmPassword] = useState("")
const [retentionEnabled, setRetentionEnabled] = useState(false)
const [retentionDays, setRetentionDays] = useState(365)
const [errors, setErrors] = useState<Record<string, string>>({})
const [isSubmitting, setIsSubmitting] = useState(false)
async function handleSubmit(e: SubmitEvent<HTMLFormElement>) {
e.preventDefault()
setErrors({})
if (!username.trim() || username.trim().length < 3) {
setErrors({ username: "Username must be at least 3 characters." })
return
}
if (password.length < 8) {
setErrors({ password: "Password must be at least 8 characters." })
return
}
if (password !== confirmPassword) {
setErrors({ confirmPassword: "Passwords do not match." })
return
}
setIsSubmitting(true)
try {
const payload: Record<string, unknown> = { password, username: username.trim() }
if (retentionEnabled && retentionDays > 0) {
payload.snapshotRetentionDays = retentionDays
}
const setupRes = await fetch("/api/auth/setup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
})
if (!setupRes.ok) {
const data = (await setupRes.json()) as { error?: string }
const msg = data.error ?? "Setup failed. Please try again."
const lowerMsg = msg.toLowerCase()
if (lowerMsg.includes("username")) {
setErrors({ username: msg })
} else if (lowerMsg.includes("match")) {
setErrors({ confirmPassword: msg })
} else if (lowerMsg.includes("8 char") || lowerMsg.includes("password")) {
setErrors({ password: msg })
} else {
setErrors({ form: msg })
}
return
}
const loginRes = await fetch("/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password, username: username.trim() }),
})
if (!loginRes.ok) {
const data = (await loginRes.json()) as { error?: string }
setErrors({ form: data.error ?? "Login after setup failed. Please go to the login page." })
return
}
router.push("/")
} catch {
setErrors({ form: "An unexpected error occurred. Please try again." })
} finally {
setIsSubmitting(false)
}
}
return (
<div className="min-h-screen bg-base flex items-center justify-center px-4">
<div className="w-full max-w-sm">
<div className="mb-8 text-center">
<Image
src="/img/trackerTracker_logo.svg"
alt="Tracker Tracker"
width={160}
height={40}
className="mx-auto"
style={{ height: 40, width: "auto" }}
priority
/>
<H2 className="mt-6 text-secondary text-center">Create an account</H2>
</div>
<Card elevation="elevated" className="p-6">
<form onSubmit={handleSubmit} className="flex flex-col gap-5">
<Input
label="Username"
type="text"
autoComplete="username"
placeholder="Min. 3 characters"
required
value={username}
onChange={(e) => setUsername(e.target.value)}
error={errors.username}
disabled={isSubmitting}
/>
<Input
label="Master Password"
type="password"
autoComplete="new-password"
placeholder="Min. 8 characters"
value={password}
onChange={(e) => setPassword(e.target.value)}
error={errors.password}
disabled={isSubmitting}
required
/>
<Input
label="Confirm Password"
type="password"
autoComplete="new-password"
placeholder="Re-enter password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
error={errors.confirmPassword}
disabled={isSubmitting}
required
/>
<div className="border-t border-border pt-4 mt-1">
<Toggle
label="Enable snapshot retention"
description={
retentionEnabled
? `Snapshots older than ${retentionDays} days will be pruned automatically.`
: "Disabled — snapshots will be kept indefinitely. You can change this later in Settings."
}
checked={retentionEnabled}
onChange={setRetentionEnabled}
disabled={isSubmitting}
/>
{retentionEnabled && (
<Input
label="Retention (days)"
type="number"
min={SNAPSHOT_RETENTION_MIN}
max={SNAPSHOT_RETENTION_MAX}
value={String(retentionDays)}
onChange={(e) =>
setRetentionDays(
Math.max(
SNAPSHOT_RETENTION_MIN,
Math.min(SNAPSHOT_RETENTION_MAX, Number(e.target.value) || 365)
)
)
}
disabled={isSubmitting}
className="mt-3"
/>
)}
</div>
<Notice message={errors.form} />
<Button
type="submit"
variant="primary"
size="md"
className="w-full mt-1"
disabled={isSubmitting}
text={isSubmitting ? "Setting up…" : "Create Account"}
/>
</form>
</Card>
</div>
</div>
)
}