import { useState } from 'react' import { GetServerSideProps } from 'next' import { getServerSession } from 'next-auth/next' import Head from 'next/head' import Link from 'next/link' import { authOptions } from '../api/auth/[...nextauth]' import Layout from '@/components/layout' export default function ForgotPassword() { const [email, setEmail] = useState('') const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const [success, setSuccess] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) setError(null) try { const response = await fetch('/api/auth/forgot-password', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email }), }) const data = await response.json() if (!response.ok) { setError(data.message || 'An error occurred') return } setSuccess(true) } catch (err) { setError('An unexpected error occurred') } finally { setIsLoading(false) } } if (success) { return ( Check Your Email | LocalGreenChain

Check Your Email

If an account exists with that email address, we've sent you a link to reset your password.

Didn't receive the email? Check your spam folder or{' '}

Back to sign in
) } return ( Forgot Password | LocalGreenChain

Reset your password

Enter your email address and we'll send you a link to reset your password.

{error && (
{error}
)}
setEmail(e.target.value)} className="mt-1 appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-green-500 focus:border-green-500 sm:text-sm" placeholder="you@example.com" />
) } export const getServerSideProps: GetServerSideProps = async (context) => { const session = await getServerSession(context.req, context.res, authOptions) if (session) { return { redirect: { destination: '/', permanent: false, }, } } return { props: {}, } }