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 { redirect } from "next/navigation";
|
||||||
import { createClient } from "@/lib/supabase/server";
|
import { createClient } from "@/lib/supabase/server";
|
||||||
|
import { createAdminClient } from "@/lib/supabase/admin";
|
||||||
import { validateLogin } from "@/lib/validate-login";
|
import { validateLogin } from "@/lib/validate-login";
|
||||||
|
|
||||||
|
const LOGIN_FEHLER = "E-Mail oder Passwort ist falsch.";
|
||||||
|
|
||||||
export async function login(formData: FormData): Promise<void> {
|
export async function login(formData: FormData): Promise<void> {
|
||||||
const validation = validateLogin({
|
const validation = validateLogin({
|
||||||
email: formData.get("email"),
|
identifier: formData.get("identifier"),
|
||||||
password: formData.get("password"),
|
password: formData.get("password"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -14,14 +17,39 @@ export async function login(formData: FormData): Promise<void> {
|
||||||
redirect(`/login?error=${encodeURIComponent(validation.error)}`);
|
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 supabase = await createClient();
|
||||||
const { error } = await supabase.auth.signInWithPassword({
|
const { error } = await supabase.auth.signInWithPassword({
|
||||||
email: validation.email,
|
email,
|
||||||
password: validation.password,
|
password: validation.password,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
redirect(`/login?error=${encodeURIComponent("E-Mail oder Passwort ist falsch.")}`);
|
redirect(`/login?error=${encodeURIComponent(LOGIN_FEHLER)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
redirect("/dashboard");
|
redirect("/dashboard");
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,8 @@ export default async function LoginPage({
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<form action={login}>
|
<form action={login}>
|
||||||
<FormField label="E-Mail" htmlFor="email">
|
<FormField label="E-Mail oder Benutzername" htmlFor="identifier">
|
||||||
<input id="email" name="email" type="email" required className={FIELD_CLASSES} />
|
<input id="identifier" name="identifier" type="text" required className={FIELD_CLASSES} />
|
||||||
</FormField>
|
</FormField>
|
||||||
<FormField label="Passwort" htmlFor="password">
|
<FormField label="Passwort" htmlFor="password">
|
||||||
<input id="password" name="password" type="password" required className={FIELD_CLASSES} />
|
<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";
|
import { validateLogin } from "./validate-login";
|
||||||
|
|
||||||
describe("validateLogin", () => {
|
describe("validateLogin", () => {
|
||||||
it("akzeptiert gültige E-Mail und Passwort", () => {
|
it("akzeptiert eine gueltige E-Mail und Passwort", () => {
|
||||||
const result = validateLogin({ email: "admin@example.com", password: "geheim123" });
|
const result = validateLogin({ identifier: "admin@example.com", password: "geheim123" });
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
valid: true,
|
valid: true,
|
||||||
email: "admin@example.com",
|
identifier: "admin@example.com",
|
||||||
password: "geheim123",
|
password: "geheim123",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("lehnt eine fehlende E-Mail ab", () => {
|
it("akzeptiert einen Benutzernamen ohne @", () => {
|
||||||
const result = validateLogin({ email: "", password: "geheim123" });
|
const result = validateLogin({ identifier: "anna.fahrer", password: "geheim123" });
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
valid: false,
|
valid: true,
|
||||||
error: "Bitte eine gültige E-Mail-Adresse eingeben.",
|
identifier: "anna.fahrer",
|
||||||
|
password: "geheim123",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("lehnt eine ungültige E-Mail ab", () => {
|
it("lehnt einen fehlenden Identifier ab", () => {
|
||||||
const result = validateLogin({ email: "keine-email", password: "geheim123" });
|
const result = validateLogin({ identifier: "", password: "geheim123" });
|
||||||
expect(result.valid).toBe(false);
|
expect(result).toEqual({
|
||||||
|
valid: false,
|
||||||
|
error: "Bitte E-Mail oder Benutzernamen eingeben.",
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("lehnt ein fehlendes Passwort ab", () => {
|
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({
|
expect(result).toEqual({
|
||||||
valid: false,
|
valid: false,
|
||||||
error: "Bitte ein Passwort eingeben.",
|
error: "Bitte ein Passwort eingeben.",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("trimmt Leerzeichen in der E-Mail", () => {
|
it("trimmt Leerzeichen im Identifier", () => {
|
||||||
const result = validateLogin({ email: " admin@example.com ", password: "geheim123" });
|
const result = validateLogin({ identifier: " admin@example.com ", password: "geheim123" });
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
valid: true,
|
valid: true,
|
||||||
email: "admin@example.com",
|
identifier: "admin@example.com",
|
||||||
password: "geheim123",
|
password: "geheim123",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,21 @@
|
||||||
export type LoginInput = {
|
export type LoginInput = {
|
||||||
email: FormDataEntryValue | null;
|
identifier: FormDataEntryValue | null;
|
||||||
password: FormDataEntryValue | null;
|
password: FormDataEntryValue | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LoginValidationResult =
|
export type LoginValidationResult =
|
||||||
| { valid: true; email: string; password: string }
|
| { valid: true; identifier: string; password: string }
|
||||||
| { valid: false; error: string };
|
| { valid: false; error: string };
|
||||||
|
|
||||||
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
||||||
|
|
||||||
export function validateLogin(input: LoginInput): LoginValidationResult {
|
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 : "";
|
const password = typeof input.password === "string" ? input.password : "";
|
||||||
|
|
||||||
if (!email || !EMAIL_REGEX.test(email)) {
|
if (!identifier) {
|
||||||
return { valid: false, error: "Bitte eine gültige E-Mail-Adresse eingeben." };
|
return { valid: false, error: "Bitte E-Mail oder Benutzernamen eingeben." };
|
||||||
}
|
}
|
||||||
if (!password) {
|
if (!password) {
|
||||||
return { valid: false, error: "Bitte ein Passwort eingeben." };
|
return { valid: false, error: "Bitte ein Passwort eingeben." };
|
||||||
}
|
}
|
||||||
return { valid: true, email, password };
|
return { valid: true, identifier, password };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue