forked from fahrPool/fahrPool
feat: add login page with server action and validation
This commit is contained in:
parent
7254923885
commit
63866f7f50
|
|
@ -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<void> {
|
||||||
|
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<void> {
|
||||||
|
const supabase = await createClient();
|
||||||
|
await supabase.auth.signOut();
|
||||||
|
redirect("/login");
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
import { login } from "./actions";
|
||||||
|
|
||||||
|
export default async function LoginPage({
|
||||||
|
searchParams,
|
||||||
|
}: {
|
||||||
|
searchParams: Promise<{ error?: string }>;
|
||||||
|
}) {
|
||||||
|
const { error } = await searchParams;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<h1>Anmelden</h1>
|
||||||
|
{error && <p role="alert">{error}</p>}
|
||||||
|
<form action={login}>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="email">E-Mail</label>
|
||||||
|
<input id="email" name="email" type="email" required />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="password">Passwort</label>
|
||||||
|
<input id="password" name="password" type="password" required />
|
||||||
|
</div>
|
||||||
|
<button type="submit">Anmelden</button>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -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",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -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 };
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue