diff --git a/web/app/login/actions.ts b/web/app/login/actions.ts new file mode 100644 index 0000000..8867624 --- /dev/null +++ b/web/app/login/actions.ts @@ -0,0 +1,34 @@ +"use server"; + +import { redirect } from "next/navigation"; +import { createClient } from "@/lib/supabase/server"; +import { validateLogin } from "@/lib/validate-login"; + +export async function login(formData: FormData): Promise { + const validation = validateLogin({ + email: formData.get("email"), + password: formData.get("password"), + }); + + if (!validation.valid) { + redirect(`/login?error=${encodeURIComponent(validation.error)}`); + } + + const supabase = await createClient(); + const { error } = await supabase.auth.signInWithPassword({ + email: validation.email, + password: validation.password, + }); + + if (error) { + redirect(`/login?error=${encodeURIComponent("E-Mail oder Passwort ist falsch.")}`); + } + + redirect("/dashboard"); +} + +export async function logout(): Promise { + const supabase = await createClient(); + await supabase.auth.signOut(); + redirect("/login"); +} diff --git a/web/app/login/page.tsx b/web/app/login/page.tsx new file mode 100644 index 0000000..4a67990 --- /dev/null +++ b/web/app/login/page.tsx @@ -0,0 +1,27 @@ +import { login } from "./actions"; + +export default async function LoginPage({ + searchParams, +}: { + searchParams: Promise<{ error?: string }>; +}) { + const { error } = await searchParams; + + return ( +
+

Anmelden

+ {error &&

{error}

} +
+
+ + +
+
+ + +
+ +
+
+ ); +} diff --git a/web/lib/validate-login.test.ts b/web/lib/validate-login.test.ts new file mode 100644 index 0000000..20f2915 --- /dev/null +++ b/web/lib/validate-login.test.ts @@ -0,0 +1,43 @@ +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" }); + expect(result).toEqual({ + valid: true, + email: "admin@example.com", + password: "geheim123", + }); + }); + + it("lehnt eine fehlende E-Mail ab", () => { + const result = validateLogin({ email: "", password: "geheim123" }); + expect(result).toEqual({ + valid: false, + error: "Bitte eine gültige E-Mail-Adresse eingeben.", + }); + }); + + it("lehnt eine ungültige E-Mail ab", () => { + const result = validateLogin({ email: "keine-email", password: "geheim123" }); + expect(result.valid).toBe(false); + }); + + it("lehnt ein fehlendes Passwort ab", () => { + const result = validateLogin({ email: "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" }); + expect(result).toEqual({ + valid: true, + email: "admin@example.com", + password: "geheim123", + }); + }); +}); diff --git a/web/lib/validate-login.ts b/web/lib/validate-login.ts new file mode 100644 index 0000000..a3e1ff0 --- /dev/null +++ b/web/lib/validate-login.ts @@ -0,0 +1,23 @@ +export type LoginInput = { + email: FormDataEntryValue | null; + password: FormDataEntryValue | null; +}; + +export type LoginValidationResult = + | { valid: true; email: 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 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 (!password) { + return { valid: false, error: "Bitte ein Passwort eingeben." }; + } + return { valid: true, email, password }; +}