"use client"; import InputComponent from "@/src/components/layout/InputComponent"; import AuthSidebarComponent from "../components/AuthSidebarComponent"; import { Button } from "@/src/components/ui/button"; import React, { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { Spinner } from "@/src/components/ui/spinner"; import { createClient } from "@/src/lib/supabase/client"; const ResetPasswordView = () => { // ------------- States ------------- const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [loading, setLoading] = useState(false); const [passwordError, setPasswordError] = useState(""); const [confirmPasswordError, setConfirmPasswordError] = useState(""); const [resetSuccess, setResetSuccess] = useState(false); // ------------- Hooks ------------- const router = useRouter(); const supabase = createClient(); useEffect(() => { // Ensure user is authenticated via reset link session // If not, redirect to login (invalid or expired link) supabase.auth.getUser().then(({ data }) => { if (!data.user) { router.replace("/login"); } }); }, [router, supabase]); /** * Handles password reset submission * * - Validation password rules * - Updates password via Supabase * - Signs user out after success (security best practice) * - Redirects to login after short delay * * @param {React.FormEvent} e - Form submit event * @returns {Promise} */ const handleResetPassword = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setPasswordError(""); setConfirmPasswordError(""); // Basic Password validation if (password.length < 8) { setPasswordError("Mindestens 8 Zeichen"); setLoading(false); return; } if (password !== confirmPassword) { setConfirmPasswordError("Passwörter stimmen nicht überein"); setLoading(false); return; } // Update password for the currently authenticated user const { error } = await supabase.auth.updateUser({ password }); if (error) { setPasswordError("Passwort konnte nicht gesetzt werden"); setLoading(false); return; } setResetSuccess(true); // Force logout after password change to refresh session try { await supabase.auth.signOut(); } catch (signOutError) { console.error("Sign out error:", signOutError); } // Redirect to login after success feedback setTimeout(() => { router.push("/login"); }, 2500); }; return (
{/* Sidebar */}
{/* Reset Password Formular */}

Passwort zurücksetzen

Bitte geben Sie Ihr neues Passwort ein. Es sollte mindestens 8 Zeichen lang sein.

{!resetSuccess ? (
setPassword(e.target.value)} error={passwordError} /> setConfirmPassword(e.target.value)} error={confirmPasswordError} /> ) : (

Passwort erfolgreich zurückgesetzt!

Ihr Passwort wurde erfolgreich geändert. Sie werden in Kürze zum Login weitergeleitet.

)}

Zurück zum

router.push("/login")}> Login

); }; export default ResetPasswordView;