Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions apps/web/app/(auth)/sign-in/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { FingerprintIcon } from "lucide-react";
import { FingerprintIcon, FlaskConicalIcon } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useRef, useState } from "react";
Expand All @@ -9,6 +9,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { authClient, signIn } from "@/lib/auth-client";
import { DEMO_EMAIL, DEMO_PASSWORD } from "@/lib/demo/constants";
import { useBranding } from "@/providers/branding";

const nextPath = () => {
Expand All @@ -17,7 +18,7 @@ const nextPath = () => {
};

const SignInPage = () => {
const { bytepayLogin, panelName, signUpEnabled } = useBranding();
const { bytepayLogin, demoMode, panelName, signUpEnabled } = useBranding();
const router = useRouter();
const [pending, setPending] = useState(false);
const [passkeyPending, setPasskeyPending] = useState(false);
Expand Down Expand Up @@ -65,14 +66,9 @@ const SignInPage = () => {
};
}, [router]);

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const form = new FormData(event.currentTarget);
const signInWithPassword = async (email: string, password: string) => {
setPending(true);
const { data, error } = await signIn.email({
email: String(form.get("email")),
password: String(form.get("password")),
});
const { data, error } = await signIn.email({ email, password });
if (error) {
setPending(false);
toast.error(error.message ?? "Sign in failed");
Expand All @@ -87,6 +83,15 @@ const SignInPage = () => {
router.refresh();
};

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const form = new FormData(event.currentTarget);
void signInWithPassword(
String(form.get("email")),
String(form.get("password"))
);
};

const handleBytepaySignIn = async () => {
setBytepayPending(true);
const { error } = await authClient.signIn.social({
Expand Down Expand Up @@ -185,6 +190,22 @@ const SignInPage = () => {
Sign in to manage your {panelName} servers.
</p>
</div>
{demoMode && (
<div className="border-border/60 bg-muted/40 flex flex-col gap-3 rounded-xl border p-4 text-sm">
<span className="flex items-center gap-2 font-medium">
<FlaskConicalIcon className="size-4" />
Try the demo
</span>
<Button
disabled={pending}
type="button"
variant="secondary"
onClick={() => signInWithPassword(DEMO_EMAIL, DEMO_PASSWORD)}
>
{pending ? "Signing in…" : "Sign in to the demo"}
</Button>
</div>
)}
{bytepayLogin ? (
<>
<Button
Expand Down
20 changes: 20 additions & 0 deletions apps/web/app/admin/actions/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import type { AuditAction } from "@/lib/audit";
import { BILLING_PROVIDERS } from "@/lib/billing-providers";
import { CURRENCIES } from "@/lib/billing/money";
import { prisma } from "@/lib/db";
import {
DemoSetupError,
disableDemoMode,
enableDemoMode,
} from "@/lib/demo/setup";
import { requireSuperAdmin } from "@/lib/session";
import {
generateBillingApiKey,
Expand Down Expand Up @@ -179,3 +184,18 @@ export const revokeApiKey = async (): Promise<ActionResult> => {
revalidatePath("/admin/settings");
return { ok: true };
};

export const setDemoMode = async (enabled: boolean): Promise<ActionResult> => {
const user = await requireSuperAdmin();
try {
await (enabled ? enableDemoMode() : disableDemoMode());
} catch (error) {
if (error instanceof DemoSetupError) {
return fail(error.message);
}
throw error;
}
await audit(user, "admin.settings.demo_mode_changed", { enabled });
refresh();
return { ok: true };
};
4 changes: 2 additions & 2 deletions apps/web/app/admin/nodes/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { notFound } from "next/navigation";

import { Panel, PanelCard } from "@/components/panel";
import { Badge } from "@/components/ui/badge";
import { DaemonClient } from "@/lib/daemon/client";
import { daemonFor } from "@/lib/daemon/client";
import { buildNodeDaemonConfig } from "@/lib/daemon/configuration";
import { prisma } from "@/lib/db";
import { API_URL } from "@/lib/env";
Expand All @@ -27,7 +27,7 @@ import { AllocationManager, CopyBlock, NodeDangerZone } from "./node-client";

const fetchSystem = async (node: Node) => {
try {
return { info: await new DaemonClient(node).system(), ok: true as const };
return { info: await daemonFor(node).system(), ok: true as const };
} catch (error) {
return {
error: error instanceof Error ? error.message : "unreachable",
Expand Down
141 changes: 141 additions & 0 deletions apps/web/app/admin/settings/demo-mode-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"use client";
import { FlaskConicalIcon } from "lucide-react";

import { Panel, PanelCard } from "@/components/panel";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { DEMO_EMAIL, DEMO_PASSWORD } from "@/lib/demo/constants";

import { setDemoMode } from "../actions/settings";
import { useAction } from "../components/use-action";

const DISABLED_FEATURES = [
"Account settings (profile, password, 2FA, passkeys, avatar)",
"SFTP access and file upload, download and remote URL pulls",
"Deleting the server, inviting team members and ordering servers",
];

export const DemoModeForm = ({ enabled }: { enabled: boolean }) => {
const { pending, run } = useAction();

return (
<Panel title="Demo mode">
<PanelCard className="flex flex-col gap-4 p-4">
<div className="flex flex-wrap items-start justify-between gap-3">
<div className="flex max-w-2xl flex-col gap-1">
<span className="flex items-center gap-2 text-sm font-medium">
<FlaskConicalIcon className="size-4" />
Demo account and server
{enabled ? (
<Badge variant="outline">on</Badge>
) : (
<Badge variant="secondary">off</Badge>
)}
</span>
<p className="text-muted-foreground text-sm">
Creates a shared demo account with a simulated server, so people
can try the panel without a real node. The console, power
controls, stats, files and backups all work against dummy data
held in memory. Sign-in details are shown on the sign-in page.
</p>
</div>
{enabled ? (
<div className="flex gap-2">
<AlertDialog>
<AlertDialogTrigger asChild>
<Button disabled={pending} variant="outline">
Reset demo
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Reset the demo?</AlertDialogTitle>
<AlertDialogDescription>
The demo account and server are recreated from scratch.
Anyone signed in to the demo is signed out.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={() => run(() => setDemoMode(true), "Demo reset")}
>
Reset
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button disabled={pending} variant="destructive">
Turn off
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Turn off demo mode?</AlertDialogTitle>
<AlertDialogDescription>
This deletes the demo account, its sessions, the demo
server and its backups and activity, and the demo node and
location.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={() =>
run(() => setDemoMode(false), "Demo mode turned off")
}
>
Turn off
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
) : (
<Button
disabled={pending}
onClick={() =>
run(() => setDemoMode(true), "Demo mode turned on")
}
>
{pending ? "Setting up…" : "Turn on"}
</Button>
)}
</div>

<div className="grid gap-3 md:grid-cols-2">
<div className="border-border/60 flex flex-col gap-1 rounded-xl border p-3 text-sm">
<span className="font-medium">Demo sign-in</span>
<span className="text-muted-foreground">
Email: <code className="text-foreground">{DEMO_EMAIL}</code>
</span>
<span className="text-muted-foreground">
Password: <code className="text-foreground">{DEMO_PASSWORD}</code>
</span>
</div>
<div className="border-border/60 flex flex-col gap-1 rounded-xl border p-3 text-sm">
<span className="font-medium">Disabled for the demo account</span>
<ul className="text-muted-foreground list-disc pl-4">
{DISABLED_FEATURES.map((feature) => (
<li key={feature}>{feature}</li>
))}
</ul>
</div>
</div>
</PanelCard>
</Panel>
);
};
2 changes: 2 additions & 0 deletions apps/web/app/admin/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { requireSuperAdmin } from "@/lib/session";
import { getPanelSettings } from "@/lib/settings";

import { PageBody, PageHeader } from "../../dashboard/components/page-header";
import { DemoModeForm } from "./demo-mode-form";
import { BillingForm, BrandingForm } from "./settings-client";
import type { BillingValues } from "./settings-client";

Expand Down Expand Up @@ -42,6 +43,7 @@ const AdminSettingsPage = async () => {
storeUrl: settings.storeUrl ?? "",
}}
/>
<DemoModeForm enabled={settings.demoMode} />
</div>
</PageBody>
</>
Expand Down
6 changes: 5 additions & 1 deletion apps/web/app/admin/updates/overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { cache } from "react";

import { DaemonClient } from "@/lib/daemon/client";
import { prisma } from "@/lib/db";
import { DEMO_NODE_ID } from "@/lib/demo/constants";
import {
getLatestRelease,
getUpdateBeta,
Expand All @@ -25,7 +26,10 @@ export const getUpdateOverview = cache(async () => {
const [panelLatest, agentLatest, nodes] = await Promise.all([
getLatestRelease("panel", { beta }),
getLatestRelease("agent", { beta }),
prisma.node.findMany({ orderBy: { name: "asc" } }),
prisma.node.findMany({
orderBy: { name: "asc" },
where: { id: { not: DEMO_NODE_ID } },
}),
]);
const agents = await Promise.all(nodes.map((node) => fetchAgent(node)));

Expand Down
7 changes: 7 additions & 0 deletions apps/web/app/api/account/avatar/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";

import { prisma } from "@/lib/db";
import { demoForbidden, isDemoUser } from "@/lib/demo/guard";
import { requireUser } from "@/lib/session";
import {
deleteObject,
Expand Down Expand Up @@ -54,6 +55,9 @@ export const GET = async (request: Request) => {

export const POST = async (request: Request) => {
const user = await requireUser();
if (isDemoUser(user)) {
return demoForbidden();
}
if (!storageConfigured()) {
return NextResponse.json(
{ error: "Avatar uploads are not available" },
Expand Down Expand Up @@ -102,6 +106,9 @@ export const POST = async (request: Request) => {

export const DELETE = async () => {
const user = await requireUser();
if (isDemoUser(user)) {
return demoForbidden();
}
if (!user.image) {
return NextResponse.json({ ok: true });
}
Expand Down
4 changes: 4 additions & 0 deletions apps/web/app/api/account/sftp-password/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { NextResponse } from "next/server";

import { logEvent } from "@/lib/audit";
import { prisma } from "@/lib/db";
import { demoForbidden, isDemoUser } from "@/lib/demo/guard";
import { requireUser } from "@/lib/session";
import { generateSftpPassword } from "@/lib/sftp";

export const runtime = "nodejs";

export const POST = async () => {
const user = await requireUser();
if (isDemoUser(user)) {
return demoForbidden();
}
const password = generateSftpPassword();
const passwordHash = await hashPassword(password);
await prisma.sftpCredential.upsert({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { NextResponse } from "next/server";
import { signTransferToken } from "@/lib/daemon/jwt";
import { daemonBaseUrl } from "@/lib/daemon/urls";
import { prisma } from "@/lib/db";
import { isDemoNode } from "@/lib/demo/constants";
import { demoForbidden } from "@/lib/demo/guard";
import { daemonRoute } from "@/lib/servers/route";

export const runtime = "nodejs";

export const GET = daemonRoute<{ id: string; backupId: string }>(
"admin",
async ({ access, params }) => {
if (isDemoNode(access.server.node)) {
return demoForbidden();
}
const { backupId } = params;
const backup = await prisma.backup.findFirst({
where: {
Expand Down
Loading