Files
ims/components/account/change-password-form.tsx
T
adminandClaude Sonnet 4.6 d18d29168a feat(auth): phase 3 — replace Supabase GoTrue with bcryptjs+jose
Custom auth stack: bcryptjs password hashing (cost 10, GoTrue-compatible),
jose JWT session cookies (edge-safe, 8hr TTL), new API routes for
login/logout/reset/change-password, middleware rewritten to JWT-only
verification with no DB access. All 38 protected pages and API routes
migrated from supabase.auth.getUser() to getSession(). Supabase .from()
queries retained for Phase 4. lib/db/index.ts refactored to lazy Proxy
singleton to avoid module-level throw during Next.js build.

tsc: clean, build: clean, tests: 4/4 passed

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-23 16:20:04 +08:00

114 lines
3.6 KiB
TypeScript

'use client'
import { useState } from 'react'
export default function ChangePasswordForm() {
const [current, setCurrent] = useState('')
const [newPass, setNewPass] = useState('')
const [confirm, setConfirm] = useState('')
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const [success, setSuccess] = useState(false)
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setError(null)
setSuccess(false)
// Client-side validation
if (!current || !newPass || !confirm) {
setError('All fields are required.')
return
}
if (newPass.length < 8) {
setError('Password must be at least 8 characters.')
return
}
if (newPass !== confirm) {
setError('Passwords do not match.')
return
}
if (newPass === current) {
setError('New password must differ from current password.')
return
}
setLoading(true)
const res = await fetch('/api/auth/change-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ currentPassword: current, newPassword: newPass }),
})
setLoading(false)
if (!res.ok) {
const data = await res.json()
setError(data.error ?? 'Password change failed.')
return
}
setCurrent('')
setNewPass('')
setConfirm('')
setSuccess(true)
}
return (
<div className="bg-white rounded-xl shadow-sm p-5">
<h2 className="text-base font-semibold text-gray-900 mb-4">Change Password</h2>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label className="block text-sm font-semibold text-gray-800 mb-1">
Current Password
</label>
<input
type="password"
value={current}
onChange={e => setCurrent(e.target.value)}
disabled={loading}
autoComplete="current-password"
className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50"
/>
</div>
<div>
<label className="block text-sm font-semibold text-gray-800 mb-1">
New Password
</label>
<input
type="password"
value={newPass}
onChange={e => setNewPass(e.target.value)}
disabled={loading}
autoComplete="new-password"
className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50"
/>
</div>
<div>
<label className="block text-sm font-semibold text-gray-800 mb-1">
Confirm New Password
</label>
<input
type="password"
value={confirm}
onChange={e => setConfirm(e.target.value)}
disabled={loading}
autoComplete="new-password"
className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50"
/>
</div>
{error && <p className="text-xs text-red-600 mt-1">{error}</p>}
{success && <p className="text-xs text-green-600 mt-1">Password changed successfully.</p>}
<button
type="submit"
disabled={loading}
className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm font-semibold disabled:opacity-50 hover:bg-blue-700"
>
{loading ? 'Saving…' : 'Update Password'}
</button>
</form>
</div>
)
}