diff --git a/apps/web/src/app/(auth)/reset-password/page.tsx b/apps/web/src/app/(auth)/reset-password/page.tsx new file mode 100644 index 0000000..3d96d7a --- /dev/null +++ b/apps/web/src/app/(auth)/reset-password/page.tsx @@ -0,0 +1,181 @@ +'use client'; + +import { Button } from '@tpmjs/ui/Button/Button'; +import { Input } from '@tpmjs/ui/Input/Input'; +import { Label } from '@tpmjs/ui/Label/Label'; +import Link from 'next/link'; +import { useSearchParams } from 'next/navigation'; +import { Suspense, useState } from 'react'; + +function ResetPasswordForm() { + const searchParams = useSearchParams(); + const token = searchParams.get('token'); + + const [password, setPassword] = useState(''); + const [confirmPassword, setConfirmPassword] = useState(''); + const [error, setError] = useState(''); + const [success, setSuccess] = useState(false); + const [loading, setLoading] = useState(false); + + async function handleSubmit(e: React.FormEvent) { + e.preventDefault(); + setError(''); + + if (password !== confirmPassword) { + setError('Passwords do not match'); + return; + } + + if (password.length < 8) { + setError('Password must be at least 8 characters'); + return; + } + + if (!token) { + setError('Invalid or missing reset token'); + return; + } + + setLoading(true); + + try { + const response = await fetch('/api/auth/reset-password', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + token, + newPassword: password, + }), + }); + + if (!response.ok) { + const data = await response.json(); + setError(data.message || 'Failed to reset password'); + setLoading(false); + return; + } + + setSuccess(true); + setLoading(false); + } catch (err) { + console.error('Reset password exception:', err); + setError('An unexpected error occurred'); + setLoading(false); + } + } + + if (!token) { + return ( +
+ This password reset link is invalid or has expired. +
++ + Request new reset link + +
++ Your password has been successfully reset. +
+Enter your new password below
++ Remember your password?{' '} + + Sign in + +
++ Click the button below to reset your password. If you didn't request this, you can safely ignore this email. +
+ + Reset Password + ++ This link will expire in 1 hour for security reasons. +
+ + + `, + }); + + if (error) { + console.error('Failed to send reset password email:', error); + throw new Error('Failed to send reset password email'); + } +}