diff --git a/web/app/dashboard/page.tsx b/web/app/dashboard/page.tsx new file mode 100644 index 0000000..fdc1a5f --- /dev/null +++ b/web/app/dashboard/page.tsx @@ -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 ( +
+

Dashboard

+

+ Eingeloggt als {result.profile.name} ({result.profile.role}) +

+
+ ); +} diff --git a/web/app/layout.tsx b/web/app/layout.tsx index 1da6a2f..6c7b5f1 100644 --- a/web/app/layout.tsx +++ b/web/app/layout.tsx @@ -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 ( + + +
+

Profil nicht gefunden

+

Für deinen Account existiert kein Profil. Bitte wende dich an einen Admin.

+
+ + + ); + } + return ( - - {children} + + + {result.status === "ok" && ( +
+ + + {result.profile.name} ({result.profile.role}) + + +
+ )} +
{children}
+ ); } diff --git a/web/app/page.tsx b/web/app/page.tsx index 7b947a2..a74cb27 100644 --- a/web/app/page.tsx +++ b/web/app/page.tsx @@ -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 ( -
-
- Next.js logo -
-

To get started, edit the page.tsx file.

-

- Looking for a starting point or more instructions? Head over to{" "} - - Templates - {" "} - or the{" "} - - Learning - {" "} - center. -

-
-
- - Vercel logomark - Deploy Now - - - Documentation - -
-
-
- ); + redirect("/dashboard"); } diff --git a/web/components/LogoutButton.tsx b/web/components/LogoutButton.tsx new file mode 100644 index 0000000..ef283b9 --- /dev/null +++ b/web/components/LogoutButton.tsx @@ -0,0 +1,9 @@ +import { logout } from "@/app/login/actions"; + +export function LogoutButton() { + return ( +
+ +
+ ); +} diff --git a/web/components/NavLinks.tsx b/web/components/NavLinks.tsx new file mode 100644 index 0000000..7e0f2e0 --- /dev/null +++ b/web/components/NavLinks.tsx @@ -0,0 +1,11 @@ +import Link from "next/link"; +import { canManageUsers, type Role } from "@/lib/auth/roles"; + +export function NavLinks({ role }: { role: Role }) { + return ( + + ); +} diff --git a/web/lib/auth/profile.ts b/web/lib/auth/profile.ts new file mode 100644 index 0000000..589fa5a --- /dev/null +++ b/web/lib/auth/profile.ts @@ -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 => { + 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 }; +}); diff --git a/web/lib/auth/roles.test.ts b/web/lib/auth/roles.test.ts new file mode 100644 index 0000000..b52337f --- /dev/null +++ b/web/lib/auth/roles.test.ts @@ -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); + }); +}); diff --git a/web/lib/auth/roles.ts b/web/lib/auth/roles.ts new file mode 100644 index 0000000..71fee1e --- /dev/null +++ b/web/lib/auth/roles.ts @@ -0,0 +1,5 @@ +export type Role = "admin" | "disponent" | "fahrer" | "readonly"; + +export function canManageUsers(role: Role): boolean { + return role === "admin"; +}