Skip to content

Commit

Permalink
refactor(actions): migrate to next-safe-action v7
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Aug 26, 2024
1 parent db79e93 commit 7a2a7c2
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 105 deletions.
41 changes: 22 additions & 19 deletions src/app/actions/change-shell-password-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperationResult> => {
shell.password = newPassword;
export const changeShellPasswordAction = actionClient
.schema(schema)
.action(
async ({
parsedInput: { shell, newPassword },
}): Promise<OperationResult> => {
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 };
}
);
85 changes: 44 additions & 41 deletions src/app/actions/create-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,56 @@ 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(),
distro: z.string(),
extraArgs: z.string(),
});

export const createShellAction = action(
schema,
async ({ name, distro, extraArgs }): Promise<OperationResult> => {
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<OperationResult> => {
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 };
}
);
);
26 changes: 14 additions & 12 deletions src/app/actions/login-action.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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("/");
});
4 changes: 2 additions & 2 deletions src/app/actions/logout-action.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
11 changes: 5 additions & 6 deletions src/app/actions/remove-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperationResult> => {
export const removeShellAction = actionClient
.schema(schema)
.action(async ({ parsedInput: { id } }): Promise<OperationResult> => {
const shellQueries = new ShellQueries();

const shell = await shellQueries.getShellFromId(id);
Expand All @@ -39,5 +39,4 @@ export const removeShellAction = action(
await shellQueries.deleteShell(id);
revalidatePath("/", "layout");
return { success: false };
}
);
});
26 changes: 14 additions & 12 deletions src/app/actions/signup-action.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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("/");
});
11 changes: 5 additions & 6 deletions src/app/actions/start-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperationResult> => {
export const startShellAction = actionClient
.schema(schema)
.action(async ({ parsedInput: { shell } }): Promise<OperationResult> => {
shell.running = true;
const { success, error } = await new ContainerHelper(
shell
Expand All @@ -31,5 +31,4 @@ export const startShellAction = action(

logger.error(`Failed to start ${shell.name}! Error: ${error}`);
return { success: false };
}
);
});
11 changes: 5 additions & 6 deletions src/app/actions/stop-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperationResult> => {
export const stopShellAction = actionClient
.schema(schema)
.action(async ({ parsedInput: { shell } }): Promise<OperationResult> => {
shell.running = false;
const { success, error } = await new ContainerHelper(shell).stopContainer();

Expand All @@ -29,5 +29,4 @@ export const stopShellAction = action(

logger.error(`Failed to stop ${shell.name}! Error: ${error}`);
return { success: false };
}
);
});
2 changes: 1 addition & 1 deletion src/lib/safe-action.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createSafeActionClient } from "next-safe-action";

export const action = createSafeActionClient();
export const actionClient = createSafeActionClient();

0 comments on commit 7a2a7c2

Please sign in to comment.