Updates
This commit is contained in:
parent
36f4af7065
commit
42dd622ad2
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
@ -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,110 +29,146 @@ export default function SignupForm({ new_deployment_user }: Props) {
|
||||
);
|
||||
|
||||
return (
|
||||
<Stack className="w-full items-stretch">
|
||||
<Input
|
||||
placeholder="Eg. John"
|
||||
title="First Name"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
first_name: v,
|
||||
}));
|
||||
}}
|
||||
showLabel
|
||||
/>
|
||||
<Input
|
||||
placeholder="Eg. Doe"
|
||||
title="Last Name"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
last_name: v,
|
||||
}));
|
||||
}}
|
||||
showLabel
|
||||
/>
|
||||
<Input
|
||||
placeholder="Email Address or Username"
|
||||
title="Email/Username"
|
||||
type="email"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
email: v,
|
||||
}));
|
||||
}}
|
||||
showLabel
|
||||
/>
|
||||
<Input
|
||||
placeholder="Password"
|
||||
title="Password"
|
||||
type="password"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
password: v,
|
||||
}));
|
||||
}}
|
||||
validity={{
|
||||
isValid:
|
||||
!Boolean(newUser.password?.match(/./)) ||
|
||||
!Boolean(newUser.confirmed_password?.match(/./))
|
||||
? true
|
||||
: is_password_valid,
|
||||
msg: `Passwords don't match`,
|
||||
}}
|
||||
showLabel
|
||||
/>
|
||||
<Input
|
||||
placeholder="Confirm Password"
|
||||
title="Confirm Password"
|
||||
type="password"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
confirmed_password: v,
|
||||
}));
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
}}
|
||||
>
|
||||
<Stack className="w-full items-stretch gap-6">
|
||||
<Input
|
||||
placeholder="Eg. John"
|
||||
title="First Name"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
first_name: v,
|
||||
}));
|
||||
}}
|
||||
required
|
||||
showLabel
|
||||
/>
|
||||
<Input
|
||||
placeholder="Eg. Doe"
|
||||
title="Last Name"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
last_name: v,
|
||||
}));
|
||||
}}
|
||||
showLabel
|
||||
/>
|
||||
<Input
|
||||
placeholder="Email Address"
|
||||
title="Email"
|
||||
type="email"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
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"
|
||||
type="password"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
password: v,
|
||||
}));
|
||||
}}
|
||||
validity={{
|
||||
isValid:
|
||||
!Boolean(newUser.password?.match(/./)) ||
|
||||
!Boolean(newUser.confirmed_password?.match(/./))
|
||||
? true
|
||||
: is_password_valid,
|
||||
msg: `Passwords don't match`,
|
||||
}}
|
||||
required
|
||||
showLabel
|
||||
/>
|
||||
<Input
|
||||
placeholder="Confirm Password"
|
||||
title="Confirm Password"
|
||||
type="password"
|
||||
changeHandler={(v) => {
|
||||
setNewUser((prev) => ({
|
||||
...prev,
|
||||
confirmed_password: v,
|
||||
}));
|
||||
|
||||
setIsPasswordConfirmed(v == newUser.password);
|
||||
}}
|
||||
showLabel
|
||||
/>
|
||||
<Button
|
||||
title="Login"
|
||||
onClick={() => {
|
||||
if (!is_password_valid) {
|
||||
return;
|
||||
}
|
||||
setIsPasswordConfirmed(v == newUser.password);
|
||||
}}
|
||||
showLabel
|
||||
/>
|
||||
<Button
|
||||
title="Login"
|
||||
onClick={() => {
|
||||
if (!is_password_valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!window.confirm(`Create Super Admin Account?`)) {
|
||||
return;
|
||||
}
|
||||
if (!window.confirm(`Create Super Admin Account?`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
setLoading(true);
|
||||
|
||||
fetchApi<APIReqObject, APIResponseObject>(
|
||||
`/api/auth/signup`,
|
||||
{
|
||||
method: "POST",
|
||||
body: {
|
||||
new_user: newUser,
|
||||
fetchApi<APIReqObject, APIResponseObject>(
|
||||
`/api/auth/signup`,
|
||||
{
|
||||
method: "POST",
|
||||
body: {
|
||||
new_user: newUser,
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
.then((res) => {
|
||||
console.log("res", res);
|
||||
)
|
||||
.then((res) => {
|
||||
console.log("res", res);
|
||||
|
||||
if (res.success) {
|
||||
window.location.reload();
|
||||
}
|
||||
})
|
||||
.finally(() => {});
|
||||
}}
|
||||
loading={loading}
|
||||
>
|
||||
Signup
|
||||
</Button>
|
||||
</Stack>
|
||||
if (res.success) {
|
||||
window.location.reload();
|
||||
}
|
||||
})
|
||||
.finally(() => {});
|
||||
}}
|
||||
loading={loading}
|
||||
>
|
||||
Signup
|
||||
</Button>
|
||||
</Stack>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
35
src/functions/deployment-users/setup-deployment-user.ts
Normal file
35
src/functions/deployment-users/setup-deployment-user.ts
Normal 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;
|
||||
}
|
||||
@ -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",
|
||||
|
||||
@ -230,6 +230,7 @@ export type TurboCISignupFormObject = {
|
||||
email?: string;
|
||||
password?: string;
|
||||
confirmed_password?: string;
|
||||
username?: string;
|
||||
};
|
||||
|
||||
export type TurboCIAdminAppContextType = ReturnType<typeof useAppInit>;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user