1
0
Fork 0

feat: add root layout, role-based navigation and dashboard

This commit is contained in:
Jonny 2026-07-04 16:14:53 +02:00
parent b397cb326b
commit c236e61692
8 changed files with 137 additions and 84 deletions

View File

@ -0,0 +1,18 @@
import { loadProfile } from "@/lib/auth/profile";
export default async function DashboardPage() {
const result = await loadProfile();
if (result.status !== "ok") {
return null;
}
return (
<div>
<h1>Dashboard</h1>
<p>
Eingeloggt als {result.profile.name} ({result.profile.role})
</p>
</div>
);
}

View File

@ -1,30 +1,44 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import { loadProfile } from "@/lib/auth/profile";
import { NavLinks } from "@/components/NavLinks";
import { LogoutButton } from "@/components/LogoutButton";
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
export const metadata = {
title: "Aushilfsfahrer",
};
export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
}: Readonly<{ children: React.ReactNode }>) {
const result = await loadProfile();
if (result.status === "missing-profile") {
return (
<html lang="en" className={`${geistSans.variable} ${geistMono.variable}`}>
<body>{children}</body>
<html lang="de">
<body>
<main>
<h1>Profil nicht gefunden</h1>
<p>Für deinen Account existiert kein Profil. Bitte wende dich an einen Admin.</p>
</main>
</body>
</html>
);
}
return (
<html lang="de">
<body>
{result.status === "ok" && (
<header>
<NavLinks role={result.profile.role} />
<span>
{result.profile.name} ({result.profile.role})
</span>
<LogoutButton />
</header>
)}
<main>{children}</main>
</body>
</html>
);
}

View File

@ -1,66 +1,5 @@
import Image from "next/image";
import styles from "./page.module.css";
import { redirect } from "next/navigation";
export default function Home() {
return (
<div className={styles.page}>
<main className={styles.main}>
<Image
className={styles.logo}
src="/next.svg"
alt="Next.js logo"
width={100}
height={20}
priority
/>
<div className={styles.intro}>
<h1>To get started, edit the page.tsx file.</h1>
<p>
Looking for a starting point or more instructions? Head over to{" "}
<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Templates
</a>{" "}
or the{" "}
<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Learning
</a>{" "}
center.
</p>
</div>
<div className={styles.ctas}>
<a
className={styles.primary}
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
className={styles.logo}
src="/vercel.svg"
alt="Vercel logomark"
width={16}
height={16}
/>
Deploy Now
</a>
<a
className={styles.secondary}
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</a>
</div>
</main>
</div>
);
redirect("/dashboard");
}

View File

@ -0,0 +1,9 @@
import { logout } from "@/app/login/actions";
export function LogoutButton() {
return (
<form action={logout}>
<button type="submit">Abmelden</button>
</form>
);
}

View File

@ -0,0 +1,11 @@
import Link from "next/link";
import { canManageUsers, type Role } from "@/lib/auth/roles";
export function NavLinks({ role }: { role: Role }) {
return (
<nav>
<Link href="/dashboard">Dashboard</Link>
{canManageUsers(role) && <Link href="/nutzerverwaltung">Nutzerverwaltung</Link>}
</nav>
);
}

37
web/lib/auth/profile.ts Normal file
View File

@ -0,0 +1,37 @@
import { cache } from "react";
import { createClient } from "@/lib/supabase/server";
import type { Role } from "./roles";
export type Profile = {
id: string;
name: string;
role: Role;
};
export type ProfileResult =
| { status: "unauthenticated" }
| { status: "missing-profile" }
| { status: "ok"; profile: Profile };
export const loadProfile = cache(async (): Promise<ProfileResult> => {
const supabase = await createClient();
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
return { status: "unauthenticated" };
}
const { data, error } = await supabase
.from("profiles")
.select("id, name, role")
.eq("id", user.id)
.single();
if (error || !data) {
return { status: "missing-profile" };
}
return { status: "ok", profile: data as Profile };
});

View File

@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { canManageUsers } from "./roles";
describe("canManageUsers", () => {
it("erlaubt Nutzerverwaltung für admin", () => {
expect(canManageUsers("admin")).toBe(true);
});
it("verbietet Nutzerverwaltung für disponent", () => {
expect(canManageUsers("disponent")).toBe(false);
});
it("verbietet Nutzerverwaltung für fahrer", () => {
expect(canManageUsers("fahrer")).toBe(false);
});
it("verbietet Nutzerverwaltung für readonly", () => {
expect(canManageUsers("readonly")).toBe(false);
});
});

5
web/lib/auth/roles.ts Normal file
View File

@ -0,0 +1,5 @@
export type Role = "admin" | "disponent" | "fahrer" | "readonly";
export function canManageUsers(role: Role): boolean {
return role === "admin";
}