forked from fahrPool/fahrPool
feat: Seite /passwort-aendern für die erzwungene Passwortänderung
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6011df76b2
commit
25f0f6c055
|
|
@ -0,0 +1,60 @@
|
|||
"use server";
|
||||
|
||||
import { redirect } from "next/navigation";
|
||||
import { createClient } from "@/lib/supabase/server";
|
||||
import { createAdminClient } from "@/lib/supabase/admin";
|
||||
import { PASSWORT_AENDERN_PFAD, hatPasswortAenderPflicht, validatePasswortAendern } from "@/lib/auth/passwort";
|
||||
|
||||
const FREISCHALTUNG_FEHLGESCHLAGEN =
|
||||
"Das Passwort wurde geändert, die Freischaltung ist aber fehlgeschlagen. Bitte wende dich an den Admin.";
|
||||
|
||||
function zurueckMitFehler(nachricht: string): never {
|
||||
redirect(`${PASSWORT_AENDERN_PFAD}?error=${encodeURIComponent(nachricht)}`);
|
||||
}
|
||||
|
||||
export async function aendereMeinPasswort(formData: FormData): Promise<void> {
|
||||
const supabase = await createClient();
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (!user) {
|
||||
redirect("/login");
|
||||
}
|
||||
// Freiwillige Passwortänderung gibt es (noch) nicht: nur mit gesetztem Merker.
|
||||
if (!hatPasswortAenderPflicht(user)) {
|
||||
redirect("/dashboard");
|
||||
}
|
||||
|
||||
const validation = validatePasswortAendern({
|
||||
neu: formData.get("neu"),
|
||||
wiederholung: formData.get("wiederholung"),
|
||||
});
|
||||
if (!validation.valid) {
|
||||
zurueckMitFehler(validation.error);
|
||||
}
|
||||
|
||||
const { error } = await supabase.auth.updateUser({ password: validation.passwort });
|
||||
if (error) {
|
||||
const gleichesPasswort =
|
||||
error.code === "same_password" || /different from the old password/i.test(error.message);
|
||||
zurueckMitFehler(
|
||||
gleichesPasswort
|
||||
? "Das neue Passwort muss sich vom bisherigen unterscheiden."
|
||||
: "Passwort ändern fehlgeschlagen."
|
||||
);
|
||||
}
|
||||
|
||||
// Erst nach erfolgreicher Passwortänderung freischalten; die umgekehrte
|
||||
// Reihenfolge würde die Sperre ohne neues Passwort aufheben.
|
||||
const adminClient = createAdminClient();
|
||||
const { error: freischaltungFehler } = await adminClient.auth.admin.updateUserById(user.id, {
|
||||
app_metadata: { must_change_password: null },
|
||||
});
|
||||
if (freischaltungFehler) {
|
||||
console.error("Merker must_change_password konnte nicht entfernt werden:", freischaltungFehler);
|
||||
zurueckMitFehler(FREISCHALTUNG_FEHLGESCHLAGEN);
|
||||
}
|
||||
|
||||
redirect("/dashboard");
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
import { redirect } from "next/navigation";
|
||||
import { createClient } from "@/lib/supabase/server";
|
||||
import { MIN_PASSWORT_LAENGE, hatPasswortAenderPflicht } from "@/lib/auth/passwort";
|
||||
import { aendereMeinPasswort } from "./actions";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||||
|
||||
export default async function PasswortAendernPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ error?: string }>;
|
||||
}) {
|
||||
const supabase = await createClient();
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (!user) {
|
||||
redirect("/login");
|
||||
}
|
||||
if (!hatPasswortAenderPflicht(user)) {
|
||||
redirect("/dashboard");
|
||||
}
|
||||
|
||||
const { error } = await searchParams;
|
||||
|
||||
return (
|
||||
<div className="flex flex-1 items-center justify-center px-4 py-8">
|
||||
<Card className="w-full max-w-sm">
|
||||
<h1 className="mb-2 text-center text-xl font-semibold text-asphalt-grey">Neues Passwort festlegen</h1>
|
||||
<p className="mb-6 text-center text-asphalt-grey">
|
||||
Dein Passwort wurde vom Admin gesetzt. Bitte lege jetzt ein eigenes Passwort fest.
|
||||
</p>
|
||||
{error && (
|
||||
<div className="mb-4">
|
||||
<Alert>{error}</Alert>
|
||||
</div>
|
||||
)}
|
||||
<form action={aendereMeinPasswort}>
|
||||
<FormField label="Neues Passwort" htmlFor="neu">
|
||||
<input
|
||||
id="neu"
|
||||
name="neu"
|
||||
type="password"
|
||||
required
|
||||
minLength={MIN_PASSWORT_LAENGE}
|
||||
autoComplete="new-password"
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<FormField label="Neues Passwort wiederholen" htmlFor="wiederholung">
|
||||
<input
|
||||
id="wiederholung"
|
||||
name="wiederholung"
|
||||
type="password"
|
||||
required
|
||||
minLength={MIN_PASSWORT_LAENGE}
|
||||
autoComplete="new-password"
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<Button type="submit">Passwort speichern</Button>
|
||||
</form>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue