2025-05-19 14:36:29 +01:00
|
|
|
import bcryptjs from "bcryptjs";
|
|
|
|
|
import { SignJWT } from "jose";
|
|
|
|
|
import { NextResponse } from "next/server";
|
2025-04-29 18:07:25 +01:00
|
|
|
|
2025-05-19 14:36:29 +01:00
|
|
|
import { env } from "@utils/env";
|
2025-05-25 18:54:00 +01:00
|
|
|
import { prisma } from "@utils/prisma";
|
2025-04-29 18:07:25 +01:00
|
|
|
|
2025-05-31 18:07:06 +01:00
|
|
|
import { passwordStrengthCheck } from "@utils/validation";
|
2025-05-30 13:27:57 +01:00
|
|
|
|
2025-05-09 10:30:12 +01:00
|
|
|
export async function POST(req: Request) {
|
2025-04-29 18:07:25 +01:00
|
|
|
try {
|
2025-05-31 18:07:06 +01:00
|
|
|
const { email, password, name } = await req.json();
|
2025-04-29 18:07:25 +01:00
|
|
|
|
2025-05-20 18:26:40 +01:00
|
|
|
const foundUser = await prisma.user.findUnique({
|
|
|
|
|
where: {
|
2025-05-31 18:07:06 +01:00
|
|
|
email: email,
|
2025-05-20 18:26:40 +01:00
|
|
|
},
|
|
|
|
|
});
|
2025-04-29 18:07:25 +01:00
|
|
|
|
|
|
|
|
if (foundUser) {
|
|
|
|
|
return NextResponse.json({ message: "Sorry, this email is already in use" }, { status: 409 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const passwordCheckResult = await passwordStrengthCheck(password);
|
|
|
|
|
|
|
|
|
|
if (passwordCheckResult === "short") {
|
|
|
|
|
return NextResponse.json({ message: "Your password is shorter than 8 characters" }, { status: 400 });
|
|
|
|
|
} else if (passwordCheckResult === "long") {
|
|
|
|
|
return NextResponse.json({ message: "Your password is longer than 16 characters" }, { status: 400 });
|
|
|
|
|
} else if (passwordCheckResult === "no lower") {
|
|
|
|
|
return NextResponse.json({ message: "Your password must contain a lowercase letters" }, { status: 400 });
|
|
|
|
|
} else if (passwordCheckResult === "no upper") {
|
|
|
|
|
return NextResponse.json({ message: "Your password must contain a uppercase letters" }, { status: 400 });
|
|
|
|
|
} else if (passwordCheckResult === "no digit") {
|
|
|
|
|
return NextResponse.json({ message: "Your password must contain a number" }, { status: 400 });
|
|
|
|
|
} else if (passwordCheckResult === "no special") {
|
|
|
|
|
return NextResponse.json({ message: "Your password must contain a special character (!@#$%^&*)" }, { status: 400 });
|
|
|
|
|
} else if (passwordCheckResult === "end of function") {
|
|
|
|
|
return NextResponse.json({ message: "Password check script failure" }, { status: 500 });
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
2025-05-31 18:07:06 +01:00
|
|
|
const newUser = await prisma.user.create({
|
2025-05-20 18:26:40 +01:00
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
email,
|
2025-05-31 18:07:06 +01:00
|
|
|
passwordHash: await bcryptjs.hash(password, 10),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Link orders with matching email to the new user
|
|
|
|
|
await prisma.order.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
email: email,
|
|
|
|
|
userId: null, // Only update orders not already linked to a user
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
userId: newUser.id,
|
2025-05-20 18:26:40 +01:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-31 18:07:06 +01:00
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
|
where: { id: newUser.id },
|
|
|
|
|
include: {
|
|
|
|
|
earthquakes: true,
|
|
|
|
|
observatories: true,
|
|
|
|
|
artefacts: true,
|
|
|
|
|
purchasedOrders: true,
|
|
|
|
|
requests: true,
|
|
|
|
|
scientist: {
|
|
|
|
|
include: {
|
|
|
|
|
superior: true,
|
|
|
|
|
subordinates: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const { passwordHash, ...userSansHash } = user!;
|
2025-05-13 22:53:17 +01:00
|
|
|
|
|
|
|
|
const secret = new TextEncoder().encode(env.JWT_SECRET_KEY);
|
2025-05-31 18:07:06 +01:00
|
|
|
const token = await new SignJWT({ userId: user!.id })
|
2025-05-13 22:53:17 +01:00
|
|
|
.setProtectedHeader({ alg: "HS256" })
|
|
|
|
|
.setExpirationTime("2w")
|
|
|
|
|
.sign(secret);
|
|
|
|
|
|
2025-05-31 18:07:06 +01:00
|
|
|
const response = NextResponse.json({ message: "Account Created", user: userSansHash }, { status: 201 });
|
2025-05-13 22:53:17 +01:00
|
|
|
response.cookies.set("jwt", token, {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
secure: process.env.NODE_ENV === "production",
|
|
|
|
|
sameSite: "strict",
|
|
|
|
|
maxAge: 3600 * 168 * 2, // 2 weeks
|
|
|
|
|
path: "/",
|
|
|
|
|
});
|
|
|
|
|
return response;
|
2025-04-29 18:07:25 +01:00
|
|
|
} catch (error) {
|
2025-05-31 18:07:06 +01:00
|
|
|
console.error("Error creating user:", error);
|
2025-04-29 18:07:25 +01:00
|
|
|
return NextResponse.json({ message: "Internal Server Error" }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error in signup endpoint:", error);
|
|
|
|
|
return NextResponse.json({ message: "Internal Server Error" }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|