This commit is contained in:
Benjamin Toby 2026-03-11 06:50:02 +00:00
parent 36f4af7065
commit 42dd622ad2
5 changed files with 183 additions and 101 deletions

View File

@ -1,12 +1,14 @@
import { AppContext } from "@/src/pages/_app";
import { TurboCISignupFormObject } from "@/src/types";
import useStatus from "@/twui/components/hooks/useStatus";
import { useState } from "react";
import { useContext, useState } from "react";
type Params = {
new_deployment_user?: boolean;
};
export default function useSignupForm({ new_deployment_user }: Params) {
const { pageProps } = useContext(AppContext);
const [newUser, setNewUser] = useState<TurboCISignupFormObject>({});
const { loading, setLoading } = useStatus();
const [isPasswordConfirmed, setIsPasswordConfirmed] = useState(false);
@ -18,5 +20,6 @@ export default function useSignupForm({ new_deployment_user }: Params) {
setLoading,
isPasswordConfirmed,
setIsPasswordConfirmed,
pageProps,
};
}

View File

@ -19,6 +19,7 @@ export default function SignupForm({ new_deployment_user }: Props) {
setLoading,
isPasswordConfirmed,
setIsPasswordConfirmed,
pageProps,
} = useSignupForm({ new_deployment_user });
const is_password_valid = Boolean(
@ -28,7 +29,12 @@ export default function SignupForm({ new_deployment_user }: Props) {
);
return (
<Stack className="w-full items-stretch">
<form
onSubmit={(e) => {
e.preventDefault();
}}
>
<Stack className="w-full items-stretch gap-6">
<Input
placeholder="Eg. John"
title="First Name"
@ -38,6 +44,7 @@ export default function SignupForm({ new_deployment_user }: Props) {
first_name: v,
}));
}}
required
showLabel
/>
<Input
@ -52,8 +59,8 @@ export default function SignupForm({ new_deployment_user }: Props) {
showLabel
/>
<Input
placeholder="Email Address or Username"
title="Email/Username"
placeholder="Email Address"
title="Email"
type="email"
changeHandler={(v) => {
setNewUser((prev) => ({
@ -61,8 +68,36 @@ export default function SignupForm({ new_deployment_user }: Props) {
email: v,
}));
}}
required
showLabel
/>
{pageProps.user.id ? (
<Input
placeholder="Username"
title="Username"
changeHandler={(v) => {
setNewUser((prev) => ({
...prev,
username: v,
}));
}}
validationRegex={/^[a-z0-9\-]{3,}$/}
info={
<>
Allowed characters:{" "}
<code>
<b>a-z, 0-9, -</b>
</code>
.
</>
}
wrapperWrapperProps={{
className: "items-start!",
}}
required
showLabel
/>
) : null}
<Input
placeholder="Password"
title="Password"
@ -81,6 +116,7 @@ export default function SignupForm({ new_deployment_user }: Props) {
: is_password_valid,
msg: `Passwords don't match`,
}}
required
showLabel
/>
<Input
@ -133,5 +169,6 @@ export default function SignupForm({ new_deployment_user }: Props) {
Signup
</Button>
</Stack>
</form>
);
}

View File

@ -0,0 +1,35 @@
import { NSQLITE_TURBOCI_ADMIN_USERS, NSQLiteTables } from "@/src/db/types";
import NSQLite from "@moduletrace/nsqlite";
import { existsSync } from "fs";
type Params = {
user_id: string | number;
};
export default async function setupDeploymentUser({ user_id }: Params) {
const target_user_res = await NSQLite.select<
NSQLITE_TURBOCI_ADMIN_USERS,
(typeof NSQLiteTables)[number]
>({
table: "users",
});
const target_user = target_user_res.singleRes;
if (!target_user?.id) {
return;
}
const { username } = target_user;
const user_dir = `/home/${username}`;
if (!existsSync(user_dir)) {
let cmd = ``;
cmd += `useradd --create-home --shell /bin/bash --comment "TurboCI Deployment user ${username}" ${username}\n`;
cmd += `passwd --lock "${username}"\n`;
}
return;
}

View File

@ -6,6 +6,7 @@ import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/
import NSQLite from "@moduletrace/nsqlite";
import type { NextApiRequest, NextApiResponse } from "next";
import userAuth from "@/src/utils/user-auth";
import { slugify } from "@/src/exports/client-exports";
export default async function handler(
req: NextApiRequest,
@ -23,6 +24,10 @@ export default async function handler(
const { new_user } = req.body as APIReqObject;
if (user?.id && !new_user?.username?.match(/^[a-z0-9\-]{3,}$/)) {
throw new Error(`Please pass a valid username`);
}
if (!new_user) {
throw new Error(`No new User Object Passed!`);
}
@ -52,7 +57,7 @@ export default async function handler(
});
}
const { first_name, email, last_name, password } = new_user;
const { first_name, email, last_name, password, username } = new_user;
const new_user_password = hashPassword({ password });
@ -67,6 +72,7 @@ export default async function handler(
email,
password: new_user_password,
is_super_admin: user?.id ? 0 : 1,
username: slugify(username),
},
],
table: "users",

View File

@ -230,6 +230,7 @@ export type TurboCISignupFormObject = {
email?: string;
password?: string;
confirmed_password?: string;
username?: string;
};
export type TurboCIAdminAppContextType = ReturnType<typeof useAppInit>;