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-19 14:36:29 +01:00
|
|
|
import { findUserByEmail, passwordStrengthCheck, readUserCsv, User, writeUserCsv } from "../functions/csvReadWrite";
|
2025-04-29 18:07:25 +01:00
|
|
|
|
2025-05-30 13:27:57 +01:00
|
|
|
// todo check if orders contain email and link if order previously made with email
|
|
|
|
|
|
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-13 22:53:17 +01:00
|
|
|
const { email, password, name } = await req.json(); // Parse incoming JSON data
|
2025-04-29 18:07:25 +01:00
|
|
|
const accessLevel = "basic";
|
|
|
|
|
|
|
|
|
|
const userData = await readUserCsv();
|
|
|
|
|
|
2025-05-20 13:53:25 +01:00
|
|
|
// todo remove console logs
|
2025-04-29 18:07:25 +01:00
|
|
|
console.log(userData);
|
|
|
|
|
console.log("Name:", name); // ! remove
|
|
|
|
|
console.log("Email:", email); // ! remove
|
|
|
|
|
console.log("Password:", password); // ! remove
|
|
|
|
|
|
2025-05-20 18:26:40 +01:00
|
|
|
const foundUser = await prisma.user.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
email: email, // use the email to uniquely identify the user
|
|
|
|
|
},
|
|
|
|
|
});
|
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-12 09:58:25 +01:00
|
|
|
const passwordHash = await bcryptjs.hash(password, 10);
|
2025-05-20 18:26:40 +01:00
|
|
|
// todo add sending back user
|
|
|
|
|
const user = await prisma.user.create({
|
|
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
email,
|
|
|
|
|
passwordHash,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-29 18:07:25 +01:00
|
|
|
await writeUserCsv(userData);
|
2025-05-13 22:53:17 +01:00
|
|
|
|
|
|
|
|
const secret = new TextEncoder().encode(env.JWT_SECRET_KEY);
|
|
|
|
|
const token = await new SignJWT({ userId: user.id })
|
|
|
|
|
.setProtectedHeader({ alg: "HS256" })
|
|
|
|
|
.setExpirationTime("2w")
|
|
|
|
|
.sign(secret);
|
|
|
|
|
|
|
|
|
|
const response = NextResponse.json({ message: "Account Created" }, { status: 201 });
|
|
|
|
|
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) {
|
|
|
|
|
console.error("Error in writting :", error);
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|