forked from fahrPool/fahrPool
feat: add root layout, role-based navigation and dashboard
This commit is contained in:
parent
b397cb326b
commit
c236e61692
|
|
@ -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>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -1,30 +1,44 @@
|
||||||
import type { Metadata } from "next";
|
|
||||||
import { Geist, Geist_Mono } from "next/font/google";
|
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { NavLinks } from "@/components/NavLinks";
|
||||||
|
import { LogoutButton } from "@/components/LogoutButton";
|
||||||
|
|
||||||
const geistSans = Geist({
|
export const metadata = {
|
||||||
variable: "--font-geist-sans",
|
title: "Aushilfsfahrer",
|
||||||
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 default function RootLayout({
|
export default async function RootLayout({
|
||||||
children,
|
children,
|
||||||
}: Readonly<{
|
}: Readonly<{ children: React.ReactNode }>) {
|
||||||
children: React.ReactNode;
|
const result = await loadProfile();
|
||||||
}>) {
|
|
||||||
|
if (result.status === "missing-profile") {
|
||||||
return (
|
return (
|
||||||
<html lang="en" className={`${geistSans.variable} ${geistMono.variable}`}>
|
<html lang="de">
|
||||||
<body>{children}</body>
|
<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>
|
</html>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,66 +1,5 @@
|
||||||
import Image from "next/image";
|
import { redirect } from "next/navigation";
|
||||||
import styles from "./page.module.css";
|
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
return (
|
redirect("/dashboard");
|
||||||
<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>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { logout } from "@/app/login/actions";
|
||||||
|
|
||||||
|
export function LogoutButton() {
|
||||||
|
return (
|
||||||
|
<form action={logout}>
|
||||||
|
<button type="submit">Abmelden</button>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -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>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -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 };
|
||||||
|
});
|
||||||
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
export type Role = "admin" | "disponent" | "fahrer" | "readonly";
|
||||||
|
|
||||||
|
export function canManageUsers(role: Role): boolean {
|
||||||
|
return role === "admin";
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue