Supabase-Clients (Browser/Server/Admin), Session-Proxy und Basis-Typen
This commit is contained in:
+49
@@ -0,0 +1,49 @@
|
||||
// Datums- und Formatierungshelfer für den Wochenplan (Mo–Fr).
|
||||
// Alle Berechnungen laufen über UTC, damit Server und Browser
|
||||
// unabhängig von der Zeitzone dieselbe Woche ermitteln.
|
||||
|
||||
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
function toDate(iso: string): Date {
|
||||
return new Date(`${iso}T00:00:00Z`);
|
||||
}
|
||||
|
||||
export function toISO(d: Date): string {
|
||||
return d.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
/** Montag der Woche, in der `iso` liegt (bzw. der aktuellen Woche). */
|
||||
export function mondayOf(iso?: string | null): string {
|
||||
const base =
|
||||
iso && ISO_DATE.test(iso)
|
||||
? toDate(iso)
|
||||
: new Date();
|
||||
const day = (base.getUTCDay() + 6) % 7; // Montag = 0
|
||||
const monday = new Date(
|
||||
Date.UTC(base.getUTCFullYear(), base.getUTCMonth(), base.getUTCDate() - day)
|
||||
);
|
||||
return toISO(monday);
|
||||
}
|
||||
|
||||
export function addDays(iso: string, days: number): string {
|
||||
const d = toDate(iso);
|
||||
d.setUTCDate(d.getUTCDate() + days);
|
||||
return toISO(d);
|
||||
}
|
||||
|
||||
export const WEEKDAYS = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"];
|
||||
|
||||
/** z. B. "13. Juli 2026" */
|
||||
export function formatDateLong(iso: string): string {
|
||||
return toDate(iso).toLocaleDateString("de-DE", {
|
||||
day: "numeric",
|
||||
month: "long",
|
||||
year: "numeric",
|
||||
timeZone: "UTC",
|
||||
});
|
||||
}
|
||||
|
||||
/** z. B. "3,50€" */
|
||||
export function formatPrice(price: number | string): string {
|
||||
return Number(price).toFixed(2).replace(".", ",") + "€";
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import "server-only";
|
||||
import { createClient as createSupabaseClient } from "@supabase/supabase-js";
|
||||
|
||||
// Service-Role-Client: umgeht RLS. Ausschließlich serverseitig verwenden
|
||||
// (Benutzerverwaltung durch den Admin).
|
||||
export function createAdminClient() {
|
||||
return createSupabaseClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.SUPABASE_SERVICE_ROLE_KEY!,
|
||||
{
|
||||
auth: { persistSession: false, autoRefreshToken: false },
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { createBrowserClient } from "@supabase/ssr";
|
||||
|
||||
// Browser-Client (Client Components). Passkey-Support ist bei Supabase
|
||||
// derzeit als experimentelles Feature markiert und muss aktiviert werden.
|
||||
export function createClient() {
|
||||
return createBrowserClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
||||
{
|
||||
auth: {
|
||||
experimental: { passkey: true },
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { createServerClient } from "@supabase/ssr";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
// Server-Client (Server Components, Route Handler, Server Actions).
|
||||
// Muss pro Request neu erzeugt werden.
|
||||
export async function createClient() {
|
||||
const cookieStore = await cookies();
|
||||
|
||||
return createServerClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
||||
{
|
||||
cookies: {
|
||||
getAll() {
|
||||
return cookieStore.getAll();
|
||||
},
|
||||
setAll(cookiesToSet) {
|
||||
try {
|
||||
cookiesToSet.forEach(({ name, value, options }) =>
|
||||
cookieStore.set(name, value, options)
|
||||
);
|
||||
} catch {
|
||||
// In Server Components dürfen keine Cookies gesetzt werden –
|
||||
// das Session-Refresh übernimmt proxy.ts.
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
export type Role = "admin" | "staff";
|
||||
|
||||
export interface Profile {
|
||||
id: string;
|
||||
email: string;
|
||||
full_name: string;
|
||||
role: Role;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Section {
|
||||
key: string;
|
||||
label: string;
|
||||
time_range: string;
|
||||
sort: number;
|
||||
}
|
||||
|
||||
export interface Dish {
|
||||
id: string;
|
||||
name: string;
|
||||
price: number;
|
||||
}
|
||||
|
||||
export interface MenuEntry {
|
||||
id: string;
|
||||
menu_date: string;
|
||||
section_key: string;
|
||||
position: number;
|
||||
dish: Dish;
|
||||
}
|
||||
|
||||
export interface CodeItem {
|
||||
code: string;
|
||||
name: string;
|
||||
sort: number;
|
||||
}
|
||||
|
||||
// Farb-Defaults (r-hofmann.de: Blau #003388, Navy #0d315c, Orange #ff6900)
|
||||
export const DEFAULT_COLORS: Record<string, string> = {
|
||||
primary: "#003388",
|
||||
primaryDark: "#0d315c",
|
||||
accent: "#ff6900",
|
||||
headerText: "#ffffff",
|
||||
rowBg: "#e3e4f1",
|
||||
rowBgAlt: "#f4f4fa",
|
||||
addBtn: "#6da54c",
|
||||
deleteBtn: "#e2371d",
|
||||
};
|
||||
|
||||
export const COLOR_VARS: Record<string, string> = {
|
||||
primary: "--c-primary",
|
||||
primaryDark: "--c-primary-dark",
|
||||
accent: "--c-accent",
|
||||
headerText: "--c-header-text",
|
||||
rowBg: "--c-row-bg",
|
||||
rowBgAlt: "--c-row-bg-alt",
|
||||
addBtn: "--c-add",
|
||||
deleteBtn: "--c-del",
|
||||
};
|
||||
|
||||
export const COLOR_LABELS: Record<string, string> = {
|
||||
primary: "Primärfarbe (Tabellenkopf)",
|
||||
primaryDark: "Primärfarbe dunkel (Kopfleiste intern)",
|
||||
accent: "Akzentfarbe",
|
||||
headerText: "Schriftfarbe im Tabellenkopf",
|
||||
rowBg: "Zellen-Hintergrund",
|
||||
rowBgAlt: "Zellen-Hintergrund (alternierend)",
|
||||
addBtn: "Button „Hinzufügen“",
|
||||
deleteBtn: "Button „Löschen“",
|
||||
};
|
||||
|
||||
export function logoUrl(path: string | null | undefined): string | null {
|
||||
if (!path) return null;
|
||||
return `${process.env.NEXT_PUBLIC_SUPABASE_URL}/storage/v1/object/public/branding/${path}`;
|
||||
}
|
||||
Reference in New Issue
Block a user