forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountSection.tsx
More file actions
172 lines (162 loc) · 5.92 KB
/
Copy pathAccountSection.tsx
File metadata and controls
172 lines (162 loc) · 5.92 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
// src/components/settings/AccountSection.tsx
"use client"
import { H3, Paragraph } from "@typography"
import { useRouter } from "next/navigation"
import { useState } from "react"
import { SettingsSection } from "@/components/settings/SettingsSection"
import { Button, Input } from "@/components/ui"
import { SaveDiscardBar } from "@/components/ui/SaveDiscardBar"
import { extractApiError } from "@/lib/extract-api-error"
import { PASSWORD_MIN, USERNAME_MIN } from "@/lib/limits"
export interface AccountSectionProps {
initialUsername: string
}
export function AccountSection({ initialUsername }: AccountSectionProps) {
const router = useRouter()
// ── Username ─────────────────────────────────────────────────────────
const [username, setUsername] = useState(initialUsername)
const [savedUsername, setSavedUsername] = useState(initialUsername)
const [savingUsername, setSavingUsername] = useState(false)
const [usernameError, setUsernameError] = useState<string | null>(null)
async function handleSaveUsername() {
setUsernameError(null)
const trimmed = username.trim()
if (trimmed && trimmed.length < USERNAME_MIN) {
setUsernameError(`Username must be at least ${USERNAME_MIN} characters`)
return
}
setSavingUsername(true)
try {
const res = await fetch("/api/settings", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: username.trim() || null }),
})
if (!res.ok) {
throw new Error(await extractApiError(res, "Save failed"))
}
const result: { username: string | null } = await res.json()
setUsername(result.username ?? "")
setSavedUsername(result.username ?? "")
} catch (err) {
setUsernameError(err instanceof Error ? err.message : "Network error")
} finally {
setSavingUsername(false)
}
}
// ── Password ─────────────────────────────────────────────────────────
const [currentPassword, setCurrentPassword] = useState("")
const [newPassword, setNewPassword] = useState("")
const [confirmPassword, setConfirmPassword] = useState("")
const [savingPassword, setSavingPassword] = useState(false)
const [passwordError, setPasswordError] = useState<string | null>(null)
async function handleChangePassword() {
setPasswordError(null)
if (!currentPassword) {
setPasswordError("Current password is required")
return
}
if (newPassword.length < PASSWORD_MIN) {
setPasswordError(`New password must be at least ${PASSWORD_MIN} characters`)
return
}
if (newPassword !== confirmPassword) {
setPasswordError("Passwords do not match")
return
}
setSavingPassword(true)
try {
const res = await fetch("/api/auth/change-password", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ currentPassword, newPassword }),
})
if (!res.ok) {
throw new Error(await extractApiError(res, "Failed"))
}
router.push("/login")
} catch (err) {
setPasswordError(err instanceof Error ? err.message : "Network error")
} finally {
setSavingPassword(false)
}
}
return (
<SettingsSection id="account" title="Account" cardClassName="flex flex-col gap-6">
{/* Change Username */}
<div className="flex flex-col gap-3">
<H3>Username</H3>
<Input
label="Login Username"
value={username}
onChange={(e) => {
setUsername(e.target.value)
setUsernameError(null)
}}
placeholder={`Min. ${USERNAME_MIN} characters (optional)`}
error={usernameError ?? undefined}
disabled={savingUsername}
/>
<Paragraph>Used to log in alongside your master password. Leave empty to remove.</Paragraph>
<div className="flex justify-end">
<Button
size="sm"
onClick={handleSaveUsername}
disabled={savingUsername || username === savedUsername}
text={savingUsername ? "Saving…" : "Save Username"}
/>
</div>
</div>
<div className="border-t border-border" />
{/* Change Password */}
<div className="flex flex-col gap-3">
<H3>Change Password</H3>
<Input
type="password"
label="Current Password"
value={currentPassword}
onChange={(e) => {
setCurrentPassword(e.target.value)
setPasswordError(null)
}}
placeholder="••••••••"
disabled={savingPassword}
/>
<Input
type="password"
label="New Password"
value={newPassword}
onChange={(e) => {
setNewPassword(e.target.value)
setPasswordError(null)
}}
placeholder="Min. 8 characters"
disabled={savingPassword}
/>
<Input
type="password"
label="Confirm New Password"
value={confirmPassword}
onChange={(e) => {
setConfirmPassword(e.target.value)
setPasswordError(null)
}}
placeholder="••••••••"
disabled={savingPassword}
/>
<Paragraph>Re-encrypts all stored API tokens. You will be logged out.</Paragraph>
<SaveDiscardBar
dirty
saving={savingPassword}
onSave={handleChangePassword}
error={passwordError}
saveLabel="Update Password"
savingLabel="Updating…"
saveDisabled={!currentPassword || !newPassword || !confirmPassword}
justify="end"
showDivider={false}
/>
</div>
</SettingsSection>
)
}