This commit is contained in:
2026-03-11 06:14:35 +00:00
parent 3b9c8a5eae
commit 36f4af7065
6 changed files with 60 additions and 12 deletions
+24 -9
View File
@@ -5,12 +5,15 @@ import hashPassword from "@moduletrace/datasquirel/dist/package-shared/functions
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
import NSQLite from "@moduletrace/nsqlite";
import type { NextApiRequest, NextApiResponse } from "next";
import userAuth from "@/src/utils/user-auth";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<APIResponseObject>,
) {
try {
const { singleRes: user } = await userAuth({ req });
if (req.method !== "POST") {
return res.json({
success: false,
@@ -35,13 +38,20 @@ export default async function handler(
table: "users",
});
if (existing_users_res.payload?.[0]?.id) {
if (existing_users_res.payload?.[0]?.id && !user?.id) {
return res.json({
success: false,
msg: `Super Admin User already exists. Other Users can be created by this user.`,
});
}
if (user?.id && !user.super_admin) {
return res.json({
success: false,
msg: `Operation not allowed!`,
});
}
const { first_name, email, last_name, password } = new_user;
const new_user_password = hashPassword({ password });
@@ -56,14 +66,12 @@ export default async function handler(
last_name,
email,
password: new_user_password,
is_super_admin: 1,
is_super_admin: user?.id ? 0 : 1,
},
],
table: "users",
});
console.log("new_user_insert_res", new_user_insert_res);
const new_user_id = new_user_insert_res.postInsertReturn?.insertId;
if (!new_user_id) {
@@ -88,12 +96,19 @@ export default async function handler(
throw new Error(`Couldn't Find Newly inserted user.`);
}
const logged_in_user = await loginUser({
res,
user_id: newly_inserted_user.id,
});
if (user?.id) {
return res.json({
success: true,
singleRes: newly_inserted_user,
});
} else {
const logged_in_user = await loginUser({
res,
user_id: newly_inserted_user.id,
});
return res.json(logged_in_user);
return res.json(logged_in_user);
}
} catch (error: any) {
return res.json({
success: false,