This commit is contained in:
Benjamin Toby 2026-03-11 06:14:35 +00:00
parent 3b9c8a5eae
commit 36f4af7065
6 changed files with 60 additions and 12 deletions

View File

@ -0,0 +1,18 @@
import { TurboCISignupFormObject } from "@/src/types";
import useStatus from "@/twui/components/hooks/useStatus";
import { useState } from "react";
export default function useDeploymentUserForm() {
const [newUser, setNewUser] = useState<TurboCISignupFormObject>({});
const { loading, setLoading } = useStatus();
const [isPasswordConfirmed, setIsPasswordConfirmed] = useState(false);
return {
newUser,
setNewUser,
loading,
setLoading,
isPasswordConfirmed,
setIsPasswordConfirmed,
};
}

View File

@ -2,6 +2,8 @@ import { Fragment, useContext } from "react";
import { AppContext } from "@/src/pages/_app";
import Divider from "@/twui/components/layout/Divider";
import AdminHero from "@/src/components/general/admin/hero";
import SignupForm from "../../../auth/signup/(partials)/signup-form";
import Stack from "@/twui/components/layout/Stack";
export default function Main() {
const { pageProps } = useContext(AppContext);
@ -10,6 +12,9 @@ export default function Main() {
<Fragment>
<AdminHero title={`Add New User`} />
<Divider />
<Stack className="grid-cell-content max-w-[600px]">
<SignupForm />
</Stack>
</Fragment>
);
}

View File

@ -2,7 +2,11 @@ import { TurboCISignupFormObject } from "@/src/types";
import useStatus from "@/twui/components/hooks/useStatus";
import { useState } from "react";
export default function useSignupForm() {
type Params = {
new_deployment_user?: boolean;
};
export default function useSignupForm({ new_deployment_user }: Params) {
const [newUser, setNewUser] = useState<TurboCISignupFormObject>({});
const { loading, setLoading } = useStatus();
const [isPasswordConfirmed, setIsPasswordConfirmed] = useState(false);

View File

@ -7,7 +7,11 @@ import { APIReqObject } from "@/src/types";
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
import { useEffect } from "react";
export default function SignupForm() {
type Props = {
new_deployment_user?: boolean;
};
export default function SignupForm({ new_deployment_user }: Props) {
const {
newUser,
setNewUser,
@ -15,7 +19,7 @@ export default function SignupForm() {
setLoading,
isPasswordConfirmed,
setIsPasswordConfirmed,
} = useSignupForm();
} = useSignupForm({ new_deployment_user });
const is_password_valid = Boolean(
isPasswordConfirmed &&

View File

@ -19,10 +19,12 @@ const schema = {
{
fieldName: "email",
dataType: "TEXT",
unique: true,
},
{
fieldName: "username",
dataType: "TEXT",
unique: true,
},
{
fieldName: "image",

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,