forked from fahrPool/fahrPool
feat: allow login via username in addition to email
This commit is contained in:
parent
3ab16f4103
commit
caf72557d5
|
|
@ -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<void> {
|
||||
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<void> {
|
|||
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");
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ export default async function LoginPage({
|
|||
</div>
|
||||
)}
|
||||
<form action={login}>
|
||||
<FormField label="E-Mail" htmlFor="email">
|
||||
<input id="email" name="email" type="email" required className={FIELD_CLASSES} />
|
||||
<FormField label="E-Mail oder Benutzername" htmlFor="identifier">
|
||||
<input id="identifier" name="identifier" type="text" required className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<FormField label="Passwort" htmlFor="password">
|
||||
<input id="password" name="password" type="password" required className={FIELD_CLASSES} />
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue