Updated signup route to remove csv stuff and add linking to previous non-account orders
This commit is contained in:
parent
90549dd021
commit
9c1c696352
@ -4,7 +4,6 @@ import { prisma } from "@utils/prisma";
|
|||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
try {
|
try {
|
||||||
// todo get earthquakes associated with observatories
|
|
||||||
const observatories = await prisma.observatory.findMany();
|
const observatories = await prisma.observatory.findMany();
|
||||||
|
|
||||||
if (observatories) {
|
if (observatories) {
|
||||||
|
|||||||
@ -5,26 +5,15 @@ import { NextResponse } from "next/server";
|
|||||||
import { env } from "@utils/env";
|
import { env } from "@utils/env";
|
||||||
import { prisma } from "@utils/prisma";
|
import { prisma } from "@utils/prisma";
|
||||||
|
|
||||||
import { findUserByEmail, passwordStrengthCheck, readUserCsv, User, writeUserCsv } from "../functions/csvReadWrite";
|
import { passwordStrengthCheck } from "@utils/validation";
|
||||||
|
|
||||||
// todo check if orders contain email and link if order previously made with email
|
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
try {
|
try {
|
||||||
const { email, password, name } = await req.json(); // Parse incoming JSON data
|
const { email, password, name } = await req.json();
|
||||||
const accessLevel = "basic";
|
|
||||||
|
|
||||||
const userData = await readUserCsv();
|
|
||||||
|
|
||||||
// todo remove console logs
|
|
||||||
console.log(userData);
|
|
||||||
console.log("Name:", name); // ! remove
|
|
||||||
console.log("Email:", email); // ! remove
|
|
||||||
console.log("Password:", password); // ! remove
|
|
||||||
|
|
||||||
const foundUser = await prisma.user.findUnique({
|
const foundUser = await prisma.user.findUnique({
|
||||||
where: {
|
where: {
|
||||||
email: email, // use the email to uniquely identify the user
|
email: email,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -50,25 +39,50 @@ export async function POST(req: Request) {
|
|||||||
return NextResponse.json({ message: "Password check script failure" }, { status: 500 });
|
return NextResponse.json({ message: "Password check script failure" }, { status: 500 });
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
const passwordHash = await bcryptjs.hash(password, 10);
|
const newUser = await prisma.user.create({
|
||||||
// todo add sending back user
|
|
||||||
const user = await prisma.user.create({
|
|
||||||
data: {
|
data: {
|
||||||
name,
|
name,
|
||||||
email,
|
email,
|
||||||
passwordHash,
|
passwordHash: await bcryptjs.hash(password, 10),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await writeUserCsv(userData);
|
// 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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
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!;
|
||||||
|
|
||||||
const secret = new TextEncoder().encode(env.JWT_SECRET_KEY);
|
const secret = new TextEncoder().encode(env.JWT_SECRET_KEY);
|
||||||
const token = await new SignJWT({ userId: user.id })
|
const token = await new SignJWT({ userId: user!.id })
|
||||||
.setProtectedHeader({ alg: "HS256" })
|
.setProtectedHeader({ alg: "HS256" })
|
||||||
.setExpirationTime("2w")
|
.setExpirationTime("2w")
|
||||||
.sign(secret);
|
.sign(secret);
|
||||||
|
|
||||||
const response = NextResponse.json({ message: "Account Created" }, { status: 201 });
|
const response = NextResponse.json({ message: "Account Created", user: userSansHash }, { status: 201 });
|
||||||
response.cookies.set("jwt", token, {
|
response.cookies.set("jwt", token, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: process.env.NODE_ENV === "production",
|
secure: process.env.NODE_ENV === "production",
|
||||||
@ -78,7 +92,7 @@ export async function POST(req: Request) {
|
|||||||
});
|
});
|
||||||
return response;
|
return response;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error in writting :", error);
|
console.error("Error creating user:", error);
|
||||||
return NextResponse.json({ message: "Internal Server Error" }, { status: 500 });
|
return NextResponse.json({ message: "Internal Server Error" }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/utils/validation.ts
Normal file
23
src/utils/validation.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
export async function passwordStrengthCheck(password: string): Promise<string> {
|
||||||
|
if (password.length < 8) {
|
||||||
|
return "short";
|
||||||
|
} else if (password.length > 16) {
|
||||||
|
return "long";
|
||||||
|
}
|
||||||
|
const lowercaseRegex = /[a-z]/;
|
||||||
|
const uppercaseRegex = /[A-Z]/;
|
||||||
|
const digitRegex = /\d/;
|
||||||
|
const specialCharRegex = /[!@#$%^&*]/;
|
||||||
|
if (!lowercaseRegex.test(password)) {
|
||||||
|
return "no lower";
|
||||||
|
} else if (!uppercaseRegex.test(password)) {
|
||||||
|
return "no upper";
|
||||||
|
} else if (!digitRegex.test(password)) {
|
||||||
|
return "no digit";
|
||||||
|
} else if (!specialCharRegex.test(password)) {
|
||||||
|
return "no special";
|
||||||
|
} else {
|
||||||
|
return "secure";
|
||||||
|
}
|
||||||
|
return "end of function";
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user