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>
94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useRouter, useSearchParams } from 'next/navigation'
|
|
import Link from 'next/link'
|
|
|
|
export default function ResetPasswordPage() {
|
|
const router = useRouter()
|
|
const searchParams = useSearchParams()
|
|
const [password, setPassword] = useState('')
|
|
const [confirm, setConfirm] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault()
|
|
setError(null)
|
|
|
|
if (password.length < 8) {
|
|
setError('Password must be at least 8 characters.')
|
|
return
|
|
}
|
|
if (password !== confirm) {
|
|
setError('Passwords do not match.')
|
|
return
|
|
}
|
|
|
|
setLoading(true)
|
|
const res = await fetch('/api/auth/reset-confirm', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: searchParams.get('token'), password }),
|
|
})
|
|
setLoading(false)
|
|
|
|
if (res.ok) {
|
|
router.push('/login?reset=success')
|
|
} else {
|
|
const data = await res.json()
|
|
setError(data.error ?? 'Reset failed')
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex min-h-screen items-center justify-center bg-gray-50 px-4">
|
|
<form onSubmit={handleSubmit} className="flex flex-col gap-4 w-full max-w-sm">
|
|
<div className="text-center">
|
|
<h1 className="text-2xl font-bold">Set new password</h1>
|
|
<p className="text-sm text-gray-500 mt-1">Choose a strong password.</p>
|
|
</div>
|
|
|
|
{error && (
|
|
<p className="bg-red-50 border border-red-200 text-red-700 text-sm rounded px-3 py-2">
|
|
{error}{' '}
|
|
{error.includes('expired') && (
|
|
<Link href="/forgot-password" className="underline">
|
|
Request new link
|
|
</Link>
|
|
)}
|
|
</p>
|
|
)}
|
|
|
|
<input
|
|
type="password"
|
|
placeholder="New password"
|
|
value={password}
|
|
onChange={e => setPassword(e.target.value)}
|
|
required
|
|
minLength={8}
|
|
autoComplete="new-password"
|
|
className="border border-gray-300 rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
|
|
/>
|
|
<input
|
|
type="password"
|
|
placeholder="Confirm new password"
|
|
value={confirm}
|
|
onChange={e => setConfirm(e.target.value)}
|
|
required
|
|
autoComplete="new-password"
|
|
className="border border-gray-300 rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
|
|
/>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 rounded-md disabled:opacity-50 transition-colors"
|
|
>
|
|
{loading ? 'Saving…' : 'Set password'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
)
|
|
}
|