-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathroute.ts
More file actions
36 lines (28 loc) · 1.07 KB
/
Copy pathroute.ts
File metadata and controls
36 lines (28 loc) · 1.07 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
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import bcrypt from "bcryptjs";
export async function POST(req: Request) {
try {
const body = await req.json();
const { token, password } = body;
if (!token || !password) {
return NextResponse.json({ error: "Missing fields" }, { status: 400 });
}
const resetToken = await prisma.passwordResetToken.findUnique({
where: { token },
});
if (!resetToken || resetToken.expires < new Date()) {
return NextResponse.json({ error: "Invalid or expired token" }, { status: 400 });
}
const hashedPassword = await bcrypt.hash(password, 10);
await prisma.user.update({
where: { email: resetToken.email },
data: { password: hashedPassword },
});
await prisma.passwordResetToken.delete({ where: { token } });
return NextResponse.json({ message: "Password reset successfully" });
} catch (error) {
console.error("Reset password error:", error);
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
}
}