7 Commits

Author SHA1 Message Date
Fran Jurmanović
8a5afee0e3 logout if unauthorized 2025-09-14 17:51:43 +02:00
Fran Jurmanović
4888db7f1a update layout 2025-08-27 22:11:22 +02:00
Fran Jurmanović
4db5d49a64 update version 2025-08-27 22:11:15 +02:00
Fran Jurmanović
bb0a5ab66d fix form actions 2025-08-27 21:52:35 +02:00
Fran Jurmanović
76d08df3da update version 2025-08-27 21:43:29 +02:00
Fran Jurmanović
fac61ef678 resolve server actions 2025-08-27 21:43:03 +02:00
Fran Jurmanović
55e0370004 update login error message 2025-08-27 21:33:28 +02:00
14 changed files with 97 additions and 29 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "acc-server-manager-web", "name": "acc-server-manager-web",
"version": "0.20.0", "version": "0.20.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "acc-server-manager-web", "name": "acc-server-manager-web",
"version": "0.20.0", "version": "0.20.2",
"dependencies": { "dependencies": {
"@date-fns/utc": "^2.1.1", "@date-fns/utc": "^2.1.1",
"@hookform/resolvers": "^5.2.1", "@hookform/resolvers": "^5.2.1",

View File

@@ -1,6 +1,6 @@
{ {
"name": "acc-server-manager-web", "name": "acc-server-manager-web",
"version": "0.20.0", "version": "0.20.2",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next dev --turbopack", "dev": "next dev --turbopack",

View File

@@ -13,7 +13,7 @@ export default async function DashboardPage() {
return ( return (
<div className="min-h-screen bg-gray-900 text-white"> <div className="min-h-screen bg-gray-900 text-white">
<header className="bg-gray-800 shadow-md"> <header className="bg-gray-800 shadow-md">
<div className="mx-auto flex max-w-7xl items-center justify-between px-4 py-4 sm:px-6 lg:px-8"> <div className="mx-auto flex max-w-[120rem] items-center justify-between px-4 py-4 sm:px-6 lg:px-8">
<h1 className="text-2xl font-bold">ACC Server Manager</h1> <h1 className="text-2xl font-bold">ACC Server Manager</h1>
<div className="flex items-center space-x-4"> <div className="flex items-center space-x-4">
{hasPermission(session.user!, 'membership.view') && ( {hasPermission(session.user!, 'membership.view') && (
@@ -61,7 +61,7 @@ export default async function DashboardPage() {
</div> </div>
</header> </header>
<main className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8"> <main className="mx-auto max-w-[120rem] px-4 py-8 sm:px-6 lg:px-8">
<div className="mb-6 flex items-center justify-between"> <div className="mb-6 flex items-center justify-between">
<h2 className="text-xl font-semibold">Your Servers</h2> <h2 className="text-xl font-semibold">Your Servers</h2>
<RefreshButton /> <RefreshButton />

View File

@@ -27,7 +27,7 @@ export default async function ServerPage({ params }: ServerPageProps) {
return ( return (
<div className="min-h-screen bg-gray-900 text-white"> <div className="min-h-screen bg-gray-900 text-white">
<div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8"> <div className="mx-auto max-w-[120rem] px-4 py-8 sm:px-6 lg:px-8">
<ServerHeader server={server} /> <ServerHeader server={server} />
<div className="mt-8"> <div className="mt-8">

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 126 KiB

View File

@@ -1,6 +1,15 @@
import { loginAction } from '@/lib/actions/auth'; 'use client';
import { loginAction, LoginResult } from '@/lib/actions/auth';
import { useActionState } from 'react';
const initialState: LoginResult = {
message: '',
success: true
};
export default function LoginPage() { export default function LoginPage() {
const [state, formAction] = useActionState(loginAction, initialState);
return ( return (
<div className="flex min-h-screen items-center justify-center bg-gray-900 px-4"> <div className="flex min-h-screen items-center justify-center bg-gray-900 px-4">
<div className="w-full max-w-md space-y-8 rounded-lg bg-gray-800 p-8 shadow-lg"> <div className="w-full max-w-md space-y-8 rounded-lg bg-gray-800 p-8 shadow-lg">
@@ -8,8 +17,13 @@ export default function LoginPage() {
<h1 className="text-3xl font-bold text-white">ACC Server Manager</h1> <h1 className="text-3xl font-bold text-white">ACC Server Manager</h1>
<p className="mt-2 text-gray-400">Sign in to manage your servers</p> <p className="mt-2 text-gray-400">Sign in to manage your servers</p>
</div> </div>
{state?.success ? null : (
<div className="rounded-md border border-red-700 bg-red-900/50 p-3 text-sm text-red-200">
{state?.message}
</div>
)}
<form action={loginAction} className="space-y-6"> <form action={formAction} className="space-y-6">
<div> <div>
<label htmlFor="username" className="mb-2 block text-sm font-medium text-gray-300"> <label htmlFor="username" className="mb-2 block text-sm font-medium text-gray-300">
Username Username

View File

@@ -80,7 +80,7 @@ export function UserManagementTable({ initialData, roles, currentUser }: UserMan
return ( return (
<> <>
<header className="bg-gray-800 shadow-md"> <header className="bg-gray-800 shadow-md">
<div className="mx-auto flex max-w-7xl items-center justify-between px-4 py-4 sm:px-6 lg:px-8"> <div className="mx-auto flex max-w-[120rem] items-center justify-between px-4 py-4 sm:px-6 lg:px-8">
<div className="flex items-center space-x-4"> <div className="flex items-center space-x-4">
<Link href="/dashboard" className="text-gray-300 hover:text-white"> <Link href="/dashboard" className="text-gray-300 hover:text-white">
<svg <svg
@@ -111,7 +111,7 @@ export function UserManagementTable({ initialData, roles, currentUser }: UserMan
</div> </div>
</header> </header>
<main className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8"> <main className="mx-auto max-w-[120rem] px-4 py-8 sm:px-6 lg:px-8">
{/* Filters */} {/* Filters */}
<div className="mb-6 rounded-lg border border-gray-700 bg-gray-800 p-4"> <div className="mb-6 rounded-lg border border-gray-700 bg-gray-800 p-4">
<h2 className="mb-3 text-lg font-semibold">Filters</h2> <h2 className="mb-3 text-lg font-semibold">Filters</h2>

View File

@@ -1,6 +1,10 @@
import Link from 'next/link'; import Link from 'next/link';
import { Server, ServiceStatus, getStatusColor, serviceStatusToString } from '@/lib/types'; import { Server, ServiceStatus, getStatusColor, serviceStatusToString } from '@/lib/types';
import { startServerAction, stopServerAction, restartServerAction } from '@/lib/actions/servers'; import {
startServerEventAction,
restartServerEventAction,
stopServerEventAction
} from '@/lib/actions/servers';
interface ServerCardProps { interface ServerCardProps {
server: Server; server: Server;
@@ -49,7 +53,7 @@ export function ServerCard({ server }: ServerCardProps) {
</Link> </Link>
<div className="flex justify-between gap-2 bg-gray-900 px-4 py-3"> <div className="flex justify-between gap-2 bg-gray-900 px-4 py-3">
<form action={startServerAction.bind(null, server.id)}> <form action={startServerEventAction.bind(null, server.id)}>
<button <button
type="submit" type="submit"
disabled={server.status === ServiceStatus.Running} disabled={server.status === ServiceStatus.Running}
@@ -59,7 +63,7 @@ export function ServerCard({ server }: ServerCardProps) {
</button> </button>
</form> </form>
<form action={restartServerAction.bind(null, server.id)}> <form action={restartServerEventAction.bind(null, server.id)}>
<button <button
type="submit" type="submit"
disabled={server.status === ServiceStatus.Stopped} disabled={server.status === ServiceStatus.Stopped}
@@ -69,7 +73,7 @@ export function ServerCard({ server }: ServerCardProps) {
</button> </button>
</form> </form>
<form action={stopServerAction.bind(null, server.id)}> <form action={stopServerEventAction.bind(null, server.id)}>
<button <button
type="submit" type="submit"
disabled={server.status === ServiceStatus.Stopped} disabled={server.status === ServiceStatus.Stopped}

View File

@@ -1,6 +1,10 @@
import Link from 'next/link'; import Link from 'next/link';
import { Server, getStatusColor, serviceStatusToString, ServiceStatus } from '@/lib/types/server'; import { Server, getStatusColor, serviceStatusToString, ServiceStatus } from '@/lib/types/server';
import { startServerAction, stopServerAction, restartServerAction } from '@/lib/actions/servers'; import {
startServerEventAction,
restartServerEventAction,
stopServerEventAction
} from '@/lib/actions/servers';
interface ServerHeaderProps { interface ServerHeaderProps {
server: Server; server: Server;
@@ -49,7 +53,7 @@ export function ServerHeader({ server }: ServerHeaderProps) {
</div> </div>
<div className="flex space-x-3"> <div className="flex space-x-3">
<form action={startServerAction.bind(null, server.id)}> <form action={startServerEventAction.bind(null, server.id)}>
<button <button
type="submit" type="submit"
disabled={server.status === ServiceStatus.Running} disabled={server.status === ServiceStatus.Running}
@@ -59,7 +63,7 @@ export function ServerHeader({ server }: ServerHeaderProps) {
</button> </button>
</form> </form>
<form action={restartServerAction.bind(null, server.id)}> <form action={restartServerEventAction.bind(null, server.id)}>
<button <button
type="submit" type="submit"
disabled={server.status === ServiceStatus.Stopped} disabled={server.status === ServiceStatus.Stopped}
@@ -69,7 +73,7 @@ export function ServerHeader({ server }: ServerHeaderProps) {
</button> </button>
</form> </form>
<form action={stopServerAction.bind(null, server.id)}> <form action={stopServerEventAction.bind(null, server.id)}>
<button <button
type="submit" type="submit"
disabled={server.status === ServiceStatus.Stopped} disabled={server.status === ServiceStatus.Stopped}

View File

@@ -40,13 +40,13 @@ export function StatisticsDashboard({ stats }: StatisticsDashboardProps) {
</div> </div>
{/* Charts */} {/* Charts */}
<div className="grid grid-cols-1 gap-6 lg:grid-cols-2"> <div className="grid grid-cols-12 gap-4">
<div className="rounded-lg bg-gray-800 p-6"> <div className="col-span-9 rounded-lg bg-gray-800 p-6">
<h3 className="mb-4 text-lg font-medium text-white">Player Count Over Time</h3> <h3 className="mb-4 text-lg font-medium text-white">Player Count Over Time</h3>
<PlayerCountChart data={stats.playerCountOverTime ?? []} /> <PlayerCountChart data={stats.playerCountOverTime ?? []} />
</div> </div>
<div className="rounded-lg bg-gray-800 p-6"> <div className="col-span-3 rounded-lg bg-gray-800 p-6">
<h3 className="mb-4 text-lg font-medium text-white">Session Types</h3> <h3 className="mb-4 text-lg font-medium text-white">Session Types</h3>
<SessionTypesChart data={stats.sessionTypes ?? []} /> <SessionTypesChart data={stats.sessionTypes ?? []} />
</div> </div>

View File

@@ -4,13 +4,21 @@ import { redirect } from 'next/navigation';
import { loginUser } from '@/lib/api/server/auth'; import { loginUser } from '@/lib/api/server/auth';
import { login, logout } from '@/lib/auth/server'; import { login, logout } from '@/lib/auth/server';
export async function loginAction(formData: FormData) { export type LoginResult = {
success: boolean;
message: string;
};
export async function loginAction(prevState: LoginResult, formData: FormData) {
try { try {
const username = formData.get('username') as string; const username = formData.get('username') as string;
const password = formData.get('password') as string; const password = formData.get('password') as string;
if (!username || !password) { if (!username || !password) {
throw new Error('Username and password are required'); return {
success: false,
message: 'Username and password are required'
};
} }
const result = await loginUser(username, password); const result = await loginUser(username, password);
@@ -18,10 +26,16 @@ export async function loginAction(formData: FormData) {
if (result.token && result.user) { if (result.token && result.user) {
await login(result.token, result.user); await login(result.token, result.user);
} else { } else {
throw new Error('Invalid credentials'); return {
success: false,
message: 'Invalid credentials'
};
} }
} catch (error) { } catch (error) {
throw new Error(error instanceof Error ? error.message : 'Authentication failed'); return {
success: false,
message: error instanceof Error ? error.message : 'Authentication failed'
};
} }
redirect('/dashboard'); redirect('/dashboard');

View File

@@ -11,10 +11,17 @@ export async function startServerAction(serverId: string) {
revalidatePath('/dashboard'); revalidatePath('/dashboard');
revalidatePath(`/dashboard/server/${serverId}`); revalidatePath(`/dashboard/server/${serverId}`);
} catch (error) { } catch (error) {
throw new Error(error instanceof Error ? error.message : 'Failed to start server'); return {
success: false,
message: error instanceof Error ? error.message : 'Failed to start server'
};
} }
} }
export async function startServerEventAction(serverId: string) {
await startServerAction(serverId);
}
export async function stopServerAction(serverId: string) { export async function stopServerAction(serverId: string) {
try { try {
const session = await requireAuth(); const session = await requireAuth();
@@ -22,10 +29,17 @@ export async function stopServerAction(serverId: string) {
revalidatePath('/dashboard'); revalidatePath('/dashboard');
revalidatePath(`/dashboard/server/${serverId}`); revalidatePath(`/dashboard/server/${serverId}`);
} catch (error) { } catch (error) {
throw new Error(error instanceof Error ? error.message : 'Failed to stop server'); return {
success: false,
message: error instanceof Error ? error.message : 'Failed to stop server'
};
} }
} }
export async function stopServerEventAction(serverId: string) {
await stopServerAction(serverId);
}
export async function restartServerAction(serverId: string) { export async function restartServerAction(serverId: string) {
try { try {
const session = await requireAuth(); const session = await requireAuth();
@@ -33,6 +47,13 @@ export async function restartServerAction(serverId: string) {
revalidatePath('/dashboard'); revalidatePath('/dashboard');
revalidatePath(`/dashboard/server/${serverId}`); revalidatePath(`/dashboard/server/${serverId}`);
} catch (error) { } catch (error) {
throw new Error(error instanceof Error ? error.message : 'Failed to restart server'); return {
success: false,
message: error instanceof Error ? error.message : 'Failed to restart server'
};
} }
} }
export async function restartServerEventAction(serverId: string) {
await restartServerAction(serverId);
}

View File

@@ -15,7 +15,11 @@ export async function loginUser(username: string, password: string) {
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(`Login failed: ${response.statusText} - ${BASE_URL}${authRoute}/login`); if (response.status === 401) {
throw new Error(`Invalid credentials`);
}
throw new Error(`Login failed: ${response.statusText}`);
} }
const { token } = await response.json(); const { token } = await response.json();

View File

@@ -1,3 +1,6 @@
import { logout } from '@/lib/auth/server';
import { redirect } from 'next/navigation';
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080'; const BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
export async function fetchServerAPI<T>( export async function fetchServerAPI<T>(
@@ -18,6 +21,10 @@ export async function fetchServerAPI<T>(
}); });
if (!response.ok) { if (!response.ok) {
if (response.status == 401) {
await logout();
redirect('/login');
}
throw new Error( throw new Error(
`API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint} - ${token}` `API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint} - ${token}`
); );