1
0
Fork 0

fix: DB-Fehler bei Firmenzugang werfen statt als gesperrt zu gelten

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jonny 2026-10-12 10:10:35 +02:00
parent 61f531552c
commit b16cc5ee6a
3 changed files with 38 additions and 5 deletions

View File

@ -9,6 +9,7 @@ import { FIRMA_GESPERRT } from "@/lib/firma/eigene-firma";
import { validateLogin } from "@/lib/validate-login";
const LOGIN_FEHLER = "E-Mail oder Passwort ist falsch.";
const LOGIN_NICHT_MOEGLICH = "Anmeldung derzeit nicht möglich. Bitte später erneut versuchen.";
export async function login(formData: FormData): Promise<void> {
const validation = validateLogin({
@ -59,7 +60,16 @@ export async function login(formData: FormData): Promise<void> {
redirect(`/login?error=${encodeURIComponent(LOGIN_FEHLER)}`);
}
const zugang = await pruefeFirmenzugang(supabase, data.user.id);
let zugang: Awaited<ReturnType<typeof pruefeFirmenzugang>> | null = null;
try {
zugang = await pruefeFirmenzugang(supabase, data.user.id);
} catch (e) {
console.error("Firmenzugang beim Login nicht pruefbar:", e);
}
if (zugang === null) {
await supabase.auth.signOut();
redirect(`/login?error=${encodeURIComponent(LOGIN_NICHT_MOEGLICH)}`);
}
if (zugang === "gesperrt") {
await supabase.auth.signOut();
redirect(`/login?error=${encodeURIComponent(FIRMA_GESPERRT)}`);

View File

@ -2,7 +2,11 @@ import { describe, expect, it } from "vitest";
import type { SupabaseClient } from "@supabase/supabase-js";
import { pruefeFirmenzugang } from "./queries";
type Antworten = { profil: { role: string; firma_id: string | null } | null; firma: { id: string } | null };
type Antworten = {
profil: { role: string; firma_id: string | null } | null;
firma: { id: string } | null;
fehler?: "profiles" | "firmen";
};
function fakeClient(a: Antworten): SupabaseClient {
return {
@ -11,7 +15,10 @@ function fakeClient(a: Antworten): SupabaseClient {
const kette = {
select: () => kette,
eq: () => kette,
maybeSingle: async () => ({ data: daten, error: null }),
maybeSingle: async () => ({
data: a.fehler === tabelle ? null : daten,
error: a.fehler === tabelle ? { message: "db weg" } : null,
}),
};
return kette;
},
@ -31,6 +38,16 @@ describe("pruefeFirmenzugang", () => {
it("Nutzer ohne Firma", async () => {
expect(await pruefeFirmenzugang(fakeClient({ profil: { role: "readonly", firma_id: null }, firma: null }), "u")).toBe("gesperrt");
});
it("wirft bei profiles-Fehler", async () => {
await expect(
pruefeFirmenzugang(fakeClient({ profil: null, firma: null, fehler: "profiles" }), "u"),
).rejects.toThrow("Profil laden fehlgeschlagen");
});
it("wirft bei firmen-Fehler", async () => {
await expect(
pruefeFirmenzugang(fakeClient({ profil: { role: "fahrer", firma_id: "f" }, firma: null, fehler: "firmen" }), "u"),
).rejects.toThrow("Firma laden fehlgeschlagen");
});
it("kein Profil", async () => {
expect(await pruefeFirmenzugang(fakeClient({ profil: null, firma: null }), "u")).toBe("gesperrt");
});

View File

@ -16,7 +16,10 @@ const FIRMA_SELECT = "id, name, logo_pfad, whatsapp_empfaenger, farbe_primaer, f
// Mit dem Nutzer-Client liefert RLS nur die eigene, aktive Firma: null heisst
// fremd, deaktiviert oder nicht vorhanden.
export async function ladeFirma(client: SupabaseClient, firmaId: string): Promise<Firma | null> {
const { data } = await client.from("firmen").select(FIRMA_SELECT).eq("id", firmaId).maybeSingle();
const { data, error } = await client.from("firmen").select(FIRMA_SELECT).eq("id", firmaId).maybeSingle();
if (error) {
throw new Error(`Firma laden fehlgeschlagen: ${error.message}`);
}
return (data as Firma | null) ?? null;
}
@ -38,7 +41,10 @@ export async function ladeFirmaZumAuftrag(client: SupabaseClient, auftragId: str
export type Firmenzugang = "ok" | "superadmin" | "gesperrt";
export async function pruefeFirmenzugang(client: SupabaseClient, userId: string): Promise<Firmenzugang> {
const { data: profil } = await client.from("profiles").select("role, firma_id").eq("id", userId).maybeSingle();
const { data: profil, error } = await client.from("profiles").select("role, firma_id").eq("id", userId).maybeSingle();
if (error) {
throw new Error(`Profil laden fehlgeschlagen: ${error.message}`);
}
const p = profil as { role: string; firma_id: string | null } | null;
if (!p) return "gesperrt";
if (p.role === "superadmin") return "superadmin";