From caf72557d54b0277d02d60296363faf17db8526e Mon Sep 17 00:00:00 2001 From: Jonny Date: Fri, 21 Aug 2026 00:53:40 +0200 Subject: [PATCH] feat: allow login via username in addition to email --- web/app/login/actions.ts | 34 +++++++++++++++++++++++++++++++--- web/app/login/page.tsx | 4 ++-- web/lib/validate-login.test.ts | 32 ++++++++++++++++++-------------- web/lib/validate-login.ts | 14 ++++++-------- 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/web/app/login/actions.ts b/web/app/login/actions.ts index 8867624..4cdfe2a 100644 --- a/web/app/login/actions.ts +++ b/web/app/login/actions.ts @@ -2,11 +2,14 @@ import { redirect } from "next/navigation"; import { createClient } from "@/lib/supabase/server"; +import { createAdminClient } from "@/lib/supabase/admin"; import { validateLogin } from "@/lib/validate-login"; +const LOGIN_FEHLER = "E-Mail oder Passwort ist falsch."; + export async function login(formData: FormData): Promise { const validation = validateLogin({ - email: formData.get("email"), + identifier: formData.get("identifier"), password: formData.get("password"), }); @@ -14,14 +17,39 @@ export async function login(formData: FormData): Promise { redirect(`/login?error=${encodeURIComponent(validation.error)}`); } + let email = validation.identifier; + if (!email.includes("@")) { + // Service-Role-Lookup noetig: profiles ist per RLS vor dem Login nicht + // lesbar. Absichtlich derselbe generische Fehler wie bei falschem + // Passwort (kein "Benutzername nicht gefunden"), damit sich nicht per + // Fehlermeldung durchprobieren laesst, welche Benutzernamen existieren. + const adminClient = createAdminClient(); + const { data: profil } = await adminClient + .from("profiles") + .select("id") + .eq("username", email.toLowerCase()) + .maybeSingle(); + + if (!profil) { + redirect(`/login?error=${encodeURIComponent(LOGIN_FEHLER)}`); + } + + const { data: authNutzer, error: nutzerFehler } = await adminClient.auth.admin.getUserById(profil.id); + if (nutzerFehler || !authNutzer.user?.email) { + redirect(`/login?error=${encodeURIComponent(LOGIN_FEHLER)}`); + } + + email = authNutzer.user.email; + } + const supabase = await createClient(); const { error } = await supabase.auth.signInWithPassword({ - email: validation.email, + email, password: validation.password, }); if (error) { - redirect(`/login?error=${encodeURIComponent("E-Mail oder Passwort ist falsch.")}`); + redirect(`/login?error=${encodeURIComponent(LOGIN_FEHLER)}`); } redirect("/dashboard"); diff --git a/web/app/login/page.tsx b/web/app/login/page.tsx index 1c7ddd1..99b8ffc 100644 --- a/web/app/login/page.tsx +++ b/web/app/login/page.tsx @@ -21,8 +21,8 @@ export default async function LoginPage({ )}
- - + + diff --git a/web/lib/validate-login.test.ts b/web/lib/validate-login.test.ts index 20f2915..7759c4b 100644 --- a/web/lib/validate-login.test.ts +++ b/web/lib/validate-login.test.ts @@ -2,41 +2,45 @@ import { describe, expect, it } from "vitest"; import { validateLogin } from "./validate-login"; describe("validateLogin", () => { - it("akzeptiert gültige E-Mail und Passwort", () => { - const result = validateLogin({ email: "admin@example.com", password: "geheim123" }); + it("akzeptiert eine gueltige E-Mail und Passwort", () => { + const result = validateLogin({ identifier: "admin@example.com", password: "geheim123" }); expect(result).toEqual({ valid: true, - email: "admin@example.com", + identifier: "admin@example.com", password: "geheim123", }); }); - it("lehnt eine fehlende E-Mail ab", () => { - const result = validateLogin({ email: "", password: "geheim123" }); + it("akzeptiert einen Benutzernamen ohne @", () => { + const result = validateLogin({ identifier: "anna.fahrer", password: "geheim123" }); expect(result).toEqual({ - valid: false, - error: "Bitte eine gültige E-Mail-Adresse eingeben.", + valid: true, + identifier: "anna.fahrer", + password: "geheim123", }); }); - it("lehnt eine ungültige E-Mail ab", () => { - const result = validateLogin({ email: "keine-email", password: "geheim123" }); - expect(result.valid).toBe(false); + it("lehnt einen fehlenden Identifier ab", () => { + const result = validateLogin({ identifier: "", password: "geheim123" }); + expect(result).toEqual({ + valid: false, + error: "Bitte E-Mail oder Benutzernamen eingeben.", + }); }); it("lehnt ein fehlendes Passwort ab", () => { - const result = validateLogin({ email: "admin@example.com", password: "" }); + const result = validateLogin({ identifier: "admin@example.com", password: "" }); expect(result).toEqual({ valid: false, error: "Bitte ein Passwort eingeben.", }); }); - it("trimmt Leerzeichen in der E-Mail", () => { - const result = validateLogin({ email: " admin@example.com ", password: "geheim123" }); + it("trimmt Leerzeichen im Identifier", () => { + const result = validateLogin({ identifier: " admin@example.com ", password: "geheim123" }); expect(result).toEqual({ valid: true, - email: "admin@example.com", + identifier: "admin@example.com", password: "geheim123", }); }); diff --git a/web/lib/validate-login.ts b/web/lib/validate-login.ts index a3e1ff0..6cff456 100644 --- a/web/lib/validate-login.ts +++ b/web/lib/validate-login.ts @@ -1,23 +1,21 @@ export type LoginInput = { - email: FormDataEntryValue | null; + identifier: FormDataEntryValue | null; password: FormDataEntryValue | null; }; export type LoginValidationResult = - | { valid: true; email: string; password: string } + | { valid: true; identifier: string; password: string } | { valid: false; error: string }; -const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; - export function validateLogin(input: LoginInput): LoginValidationResult { - const email = typeof input.email === "string" ? input.email.trim() : ""; + const identifier = typeof input.identifier === "string" ? input.identifier.trim() : ""; const password = typeof input.password === "string" ? input.password : ""; - if (!email || !EMAIL_REGEX.test(email)) { - return { valid: false, error: "Bitte eine gültige E-Mail-Adresse eingeben." }; + if (!identifier) { + return { valid: false, error: "Bitte E-Mail oder Benutzernamen eingeben." }; } if (!password) { return { valid: false, error: "Bitte ein Passwort eingeben." }; } - return { valid: true, email, password }; + return { valid: true, identifier, password }; }