diff --git a/src/app/actions/change-shell-password-action.ts b/src/app/actions/change-shell-password-action.ts index a5e0df8..75da4e9 100644 --- a/src/app/actions/change-shell-password-action.ts +++ b/src/app/actions/change-shell-password-action.ts @@ -4,34 +4,37 @@ import { revalidatePath } from "next/cache"; import { OperationResult } from "@/types/types"; import ContainerHelper from "@/lib/helpers/container.helper"; import { logger } from "@/lib/logger"; -import { action } from "@/lib/safe-action"; import { z } from "zod"; import { shellSchema } from "@/schemas/shellSchema"; import ShellQueries from "@/server/queries/shell/shell.queries"; +import { actionClient } from "@/lib/safe-action"; const schema = z.object({ shell: shellSchema, newPassword: z.string(), }); -export const changeShellPasswordAction = action( - schema, - async ({ shell, newPassword }): Promise => { - shell.password = newPassword; +export const changeShellPasswordAction = actionClient + .schema(schema) + .action( + async ({ + parsedInput: { shell, newPassword }, + }): Promise => { + shell.password = newPassword; - const { success, error } = await new ContainerHelper( - shell - ).changePassword(); + const { success, error } = await new ContainerHelper( + shell + ).changePassword(); - if (success) { - const shellQueries = new ShellQueries(); - logger.info("Password changed!"); - await shellQueries.changeShellPassword(shell); - revalidatePath("/", "layout"); - return { success: true }; - } + if (success) { + const shellQueries = new ShellQueries(); + logger.info("Password changed!"); + await shellQueries.changeShellPassword(shell); + revalidatePath("/", "layout"); + return { success: true }; + } - logger.error(`Error changing password! Error: ${error}`); - return { success: false }; - } -); + logger.error(`Error changing password! Error: ${error}`); + return { success: false }; + } + ); diff --git a/src/app/actions/create-action.ts b/src/app/actions/create-action.ts index b7cff59..cdbb795 100644 --- a/src/app/actions/create-action.ts +++ b/src/app/actions/create-action.ts @@ -7,8 +7,8 @@ import { generateString } from "@/utils/random"; import PortHelper from "@/lib/helpers/port.helper"; import ContainerHelper from "@/lib/helpers/container.helper"; import { z } from "zod"; -import { action } from "@/lib/safe-action"; import ShellQueries from "@/server/queries/shell/shell.queries"; +import { actionClient } from "@/lib/safe-action"; const schema = z.object({ name: z.string(), @@ -16,44 +16,47 @@ const schema = z.object({ extraArgs: z.string(), }); -export const createShellAction = action( - schema, - async ({ name, distro, extraArgs }): Promise => { - const shellQueries = new ShellQueries(); - - logger.info( - `Creating shell with name ${name}, distro ${distro}, extra arguments ${ - extraArgs.length !== 0 ? extraArgs : "none" - }...` - ); - - if (await shellQueries.checkIfShellExists(name)) { - return { success: false, shellExists: true }; - } - - const port = await new PortHelper().getAvailablePort(); - - const data = { - id: 0, - distro: distro, - name: name, - port: port, - password: generateString(8), - extraArgs: extraArgs, - running: true, - }; - - const { success, error } = await new ContainerHelper( - data - ).createContainer(); - - if (success) { - logger.info("Server ready!"); - await shellQueries.addShell(data); - revalidatePath("/", "layout"); - return { success: true }; +export const createShellAction = actionClient + .schema(schema) + .action( + async ({ + parsedInput: { name, distro, extraArgs }, + }): Promise => { + const shellQueries = new ShellQueries(); + + logger.info( + `Creating shell with name ${name}, distro ${distro}, extra arguments ${ + extraArgs.length !== 0 ? extraArgs : "none" + }...` + ); + + if (await shellQueries.checkIfShellExists(name)) { + return { success: false, shellExists: true }; + } + + const port = await new PortHelper().getAvailablePort(); + + const data = { + id: 0, + distro: distro, + name: name, + port: port, + password: generateString(8), + extraArgs: extraArgs, + running: true, + }; + + const { success, error } = await new ContainerHelper( + data + ).createContainer(); + + if (success) { + logger.info("Server ready!"); + await shellQueries.addShell(data); + revalidatePath("/", "layout"); + return { success: true }; + } + logger.warn(`Failed to bake server: ${error}`); + return { success: false }; } - logger.warn(`Failed to bake server: ${error}`); - return { success: false }; - } -); + ); diff --git a/src/app/actions/login-action.ts b/src/app/actions/login-action.ts index e32595a..f8f65e2 100644 --- a/src/app/actions/login-action.ts +++ b/src/app/actions/login-action.ts @@ -1,7 +1,7 @@ "use server"; import { getSession } from "@/lib/helpers/session.helper"; -import { action } from "@/lib/safe-action"; +import { actionClient } from "@/lib/safe-action"; import AuthQueries from "@/server/queries/auth/auth.queries"; import { compare } from "bcrypt"; import { redirect } from "next/navigation"; @@ -12,18 +12,20 @@ const schema = z.object({ password: z.string(), }); -export const loginAction = action(schema, async ({ username, password }) => { - const session = await getSession(); - const authQueries = new AuthQueries(); - const user = await authQueries.getUser(username); +export const loginAction = actionClient + .schema(schema) + .action(async ({ parsedInput: { username, password } }) => { + const session = await getSession(); + const authQueries = new AuthQueries(); + const user = await authQueries.getUser(username); - if (user === undefined) return { success: false }; + if (user === undefined) return { success: false }; - if (!(await compare(password, user.password))) return { success: false }; + if (!(await compare(password, user.password))) return { success: false }; - session.username = username; - session.isLoggedIn = true; + session.username = username; + session.isLoggedIn = true; - await session.save(); - redirect("/"); -}); + await session.save(); + redirect("/"); + }); diff --git a/src/app/actions/logout-action.ts b/src/app/actions/logout-action.ts index 288d4ee..ee93555 100644 --- a/src/app/actions/logout-action.ts +++ b/src/app/actions/logout-action.ts @@ -1,11 +1,11 @@ "use server"; import { getSession } from "@/lib/helpers/session.helper"; -import { action } from "@/lib/safe-action"; +import { actionClient } from "@/lib/safe-action"; import { redirect } from "next/navigation"; import { z } from "zod"; -export const logoutAction = action(z.void(), async () => { +export const logoutAction = actionClient.schema(z.void()).action(async () => { const session = await getSession(); session.destroy(); redirect("/login"); diff --git a/src/app/actions/remove-action.ts b/src/app/actions/remove-action.ts index c9e2ea7..7da13ff 100644 --- a/src/app/actions/remove-action.ts +++ b/src/app/actions/remove-action.ts @@ -5,16 +5,16 @@ import { logger } from "@/lib/logger"; import { revalidatePath } from "next/cache"; import ContainerHelper from "@/lib/helpers/container.helper"; import { z } from "zod"; -import { action } from "@/lib/safe-action"; import ShellQueries from "@/server/queries/shell/shell.queries"; +import { actionClient } from "@/lib/safe-action"; const schema = z.object({ id: z.number(), }); -export const removeShellAction = action( - schema, - async ({ id }): Promise => { +export const removeShellAction = actionClient + .schema(schema) + .action(async ({ parsedInput: { id } }): Promise => { const shellQueries = new ShellQueries(); const shell = await shellQueries.getShellFromId(id); @@ -39,5 +39,4 @@ export const removeShellAction = action( await shellQueries.deleteShell(id); revalidatePath("/", "layout"); return { success: false }; - } -); + }); diff --git a/src/app/actions/signup-action.ts b/src/app/actions/signup-action.ts index 722bffb..4eaee50 100644 --- a/src/app/actions/signup-action.ts +++ b/src/app/actions/signup-action.ts @@ -1,7 +1,7 @@ "use server"; import { getSession } from "@/lib/helpers/session.helper"; -import { action } from "@/lib/safe-action"; +import { actionClient } from "@/lib/safe-action"; import AuthQueries from "@/server/queries/auth/auth.queries"; import { genSalt, hash } from "bcrypt"; import { redirect } from "next/navigation"; @@ -12,17 +12,19 @@ const schema = z.object({ password: z.string(), }); -export const signupAction = action(schema, async ({ username, password }) => { - const authQueries = new AuthQueries(); - const session = await getSession(); - const salt = await genSalt(10); - const hashedPassword = await hash(password, salt); +export const signupAction = actionClient + .schema(schema) + .action(async ({ parsedInput: { username, password } }) => { + const authQueries = new AuthQueries(); + const session = await getSession(); + const salt = await genSalt(10); + const hashedPassword = await hash(password, salt); - await authQueries.addUser(username, hashedPassword); + await authQueries.addUser(username, hashedPassword); - session.username = username; - session.isLoggedIn = true; - await session.save(); + session.username = username; + session.isLoggedIn = true; + await session.save(); - redirect("/"); -}); + redirect("/"); + }); diff --git a/src/app/actions/start-action.ts b/src/app/actions/start-action.ts index 72ea16d..bad861d 100644 --- a/src/app/actions/start-action.ts +++ b/src/app/actions/start-action.ts @@ -6,16 +6,16 @@ import { revalidatePath } from "next/cache"; import ContainerHelper from "@/lib/helpers/container.helper"; import { shellSchema } from "@/schemas/shellSchema"; import { z } from "zod"; -import { action } from "@/lib/safe-action"; import ShellQueries from "@/server/queries/shell/shell.queries"; +import { actionClient } from "@/lib/safe-action"; const schema = z.object({ shell: shellSchema, }); -export const startShellAction = action( - schema, - async ({ shell }): Promise => { +export const startShellAction = actionClient + .schema(schema) + .action(async ({ parsedInput: { shell } }): Promise => { shell.running = true; const { success, error } = await new ContainerHelper( shell @@ -31,5 +31,4 @@ export const startShellAction = action( logger.error(`Failed to start ${shell.name}! Error: ${error}`); return { success: false }; - } -); + }); diff --git a/src/app/actions/stop-action.ts b/src/app/actions/stop-action.ts index a4f23b9..27bb544 100644 --- a/src/app/actions/stop-action.ts +++ b/src/app/actions/stop-action.ts @@ -5,17 +5,17 @@ import { logger } from "@/lib/logger"; import { revalidatePath } from "next/cache"; import ContainerHelper from "@/lib/helpers/container.helper"; import { z } from "zod"; -import { action } from "@/lib/safe-action"; import { shellSchema } from "@/schemas/shellSchema"; import ShellQueries from "@/server/queries/shell/shell.queries"; +import { actionClient } from "@/lib/safe-action"; const schema = z.object({ shell: shellSchema, }); -export const stopShellAction = action( - schema, - async ({ shell }): Promise => { +export const stopShellAction = actionClient + .schema(schema) + .action(async ({ parsedInput: { shell } }): Promise => { shell.running = false; const { success, error } = await new ContainerHelper(shell).stopContainer(); @@ -29,5 +29,4 @@ export const stopShellAction = action( logger.error(`Failed to stop ${shell.name}! Error: ${error}`); return { success: false }; - } -); + }); diff --git a/src/lib/safe-action.ts b/src/lib/safe-action.ts index 45214ea..7f62198 100644 --- a/src/lib/safe-action.ts +++ b/src/lib/safe-action.ts @@ -1,3 +1,3 @@ import { createSafeActionClient } from "next-safe-action"; -export const action = createSafeActionClient(); +export const actionClient = createSafeActionClient();