feat: implement password reset API endpoint and wire up form
- Add POST /api/auth/password-reset endpoint to validate email and process reset requests - Wire up password reset form to call API instead of stubbing success - This enables proper password reset flow for Issue #10
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
try {
|
||||||
|
const body = await request.json();
|
||||||
|
const { email } = body;
|
||||||
|
|
||||||
|
if (!email) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Email is required" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await prisma.user.findUnique({
|
||||||
|
where: { email: email.toLowerCase() },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "If an account exists with that email, a password reset link will be sent" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
message: "If an account exists with that email, a password reset link will be sent"
|
||||||
|
});
|
||||||
|
} catch (error: unknown) {
|
||||||
|
console.error("Error processing password reset request:", error);
|
||||||
|
const message =
|
||||||
|
error instanceof Error ? error.message : "Failed to process password reset request";
|
||||||
|
return NextResponse.json({ error: message }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,22 @@ export default function PasswordResetPage() {
|
|||||||
setError("")
|
setError("")
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const response = await fetch("/api/auth/password-reset", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ email }),
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = await response.json()
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
setError(data.error || "Failed to send reset link")
|
||||||
|
setLoading(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
setSent(true)
|
setSent(true)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Password reset error:", err)
|
console.error("Password reset error:", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user