Updates
This commit is contained in:
parent
3b9c8a5eae
commit
36f4af7065
@ -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,
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -2,6 +2,8 @@ import { Fragment, useContext } from "react";
|
|||||||
import { AppContext } from "@/src/pages/_app";
|
import { AppContext } from "@/src/pages/_app";
|
||||||
import Divider from "@/twui/components/layout/Divider";
|
import Divider from "@/twui/components/layout/Divider";
|
||||||
import AdminHero from "@/src/components/general/admin/hero";
|
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() {
|
export default function Main() {
|
||||||
const { pageProps } = useContext(AppContext);
|
const { pageProps } = useContext(AppContext);
|
||||||
@ -10,6 +12,9 @@ export default function Main() {
|
|||||||
<Fragment>
|
<Fragment>
|
||||||
<AdminHero title={`Add New User`} />
|
<AdminHero title={`Add New User`} />
|
||||||
<Divider />
|
<Divider />
|
||||||
|
<Stack className="grid-cell-content max-w-[600px]">
|
||||||
|
<SignupForm />
|
||||||
|
</Stack>
|
||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,11 @@ import { TurboCISignupFormObject } from "@/src/types";
|
|||||||
import useStatus from "@/twui/components/hooks/useStatus";
|
import useStatus from "@/twui/components/hooks/useStatus";
|
||||||
import { useState } from "react";
|
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 [newUser, setNewUser] = useState<TurboCISignupFormObject>({});
|
||||||
const { loading, setLoading } = useStatus();
|
const { loading, setLoading } = useStatus();
|
||||||
const [isPasswordConfirmed, setIsPasswordConfirmed] = useState(false);
|
const [isPasswordConfirmed, setIsPasswordConfirmed] = useState(false);
|
||||||
|
|||||||
@ -7,7 +7,11 @@ import { APIReqObject } from "@/src/types";
|
|||||||
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
|
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export default function SignupForm() {
|
type Props = {
|
||||||
|
new_deployment_user?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function SignupForm({ new_deployment_user }: Props) {
|
||||||
const {
|
const {
|
||||||
newUser,
|
newUser,
|
||||||
setNewUser,
|
setNewUser,
|
||||||
@ -15,7 +19,7 @@ export default function SignupForm() {
|
|||||||
setLoading,
|
setLoading,
|
||||||
isPasswordConfirmed,
|
isPasswordConfirmed,
|
||||||
setIsPasswordConfirmed,
|
setIsPasswordConfirmed,
|
||||||
} = useSignupForm();
|
} = useSignupForm({ new_deployment_user });
|
||||||
|
|
||||||
const is_password_valid = Boolean(
|
const is_password_valid = Boolean(
|
||||||
isPasswordConfirmed &&
|
isPasswordConfirmed &&
|
||||||
|
|||||||
@ -19,10 +19,12 @@ const schema = {
|
|||||||
{
|
{
|
||||||
fieldName: "email",
|
fieldName: "email",
|
||||||
dataType: "TEXT",
|
dataType: "TEXT",
|
||||||
|
unique: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
fieldName: "username",
|
fieldName: "username",
|
||||||
dataType: "TEXT",
|
dataType: "TEXT",
|
||||||
|
unique: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
fieldName: "image",
|
fieldName: "image",
|
||||||
|
|||||||
@ -5,12 +5,15 @@ import hashPassword from "@moduletrace/datasquirel/dist/package-shared/functions
|
|||||||
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
|
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||||
import NSQLite from "@moduletrace/nsqlite";
|
import NSQLite from "@moduletrace/nsqlite";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
import userAuth from "@/src/utils/user-auth";
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse<APIResponseObject>,
|
res: NextApiResponse<APIResponseObject>,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
const { singleRes: user } = await userAuth({ req });
|
||||||
|
|
||||||
if (req.method !== "POST") {
|
if (req.method !== "POST") {
|
||||||
return res.json({
|
return res.json({
|
||||||
success: false,
|
success: false,
|
||||||
@ -35,13 +38,20 @@ export default async function handler(
|
|||||||
table: "users",
|
table: "users",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (existing_users_res.payload?.[0]?.id) {
|
if (existing_users_res.payload?.[0]?.id && !user?.id) {
|
||||||
return res.json({
|
return res.json({
|
||||||
success: false,
|
success: false,
|
||||||
msg: `Super Admin User already exists. Other Users can be created by this user.`,
|
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 { first_name, email, last_name, password } = new_user;
|
||||||
|
|
||||||
const new_user_password = hashPassword({ password });
|
const new_user_password = hashPassword({ password });
|
||||||
@ -56,14 +66,12 @@ export default async function handler(
|
|||||||
last_name,
|
last_name,
|
||||||
email,
|
email,
|
||||||
password: new_user_password,
|
password: new_user_password,
|
||||||
is_super_admin: 1,
|
is_super_admin: user?.id ? 0 : 1,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
table: "users",
|
table: "users",
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("new_user_insert_res", new_user_insert_res);
|
|
||||||
|
|
||||||
const new_user_id = new_user_insert_res.postInsertReturn?.insertId;
|
const new_user_id = new_user_insert_res.postInsertReturn?.insertId;
|
||||||
|
|
||||||
if (!new_user_id) {
|
if (!new_user_id) {
|
||||||
@ -88,12 +96,19 @@ export default async function handler(
|
|||||||
throw new Error(`Couldn't Find Newly inserted user.`);
|
throw new Error(`Couldn't Find Newly inserted user.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const logged_in_user = await loginUser({
|
if (user?.id) {
|
||||||
res,
|
return res.json({
|
||||||
user_id: newly_inserted_user.id,
|
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) {
|
} catch (error: any) {
|
||||||
return res.json({
|
return res.json({
|
||||||
success: false,
|
success: false,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user