localgreenchain/components/auth/PasswordResetForm.tsx
Claude 39b6081baa
Implement comprehensive authentication system (Agent 1)
Add complete user authentication with NextAuth.js supporting:
- Email/password credentials authentication
- OAuth providers (GitHub, Google) with optional configuration
- JWT-based session management with 30-day expiry
- Role-based access control (USER, GROWER, FARM_MANAGER, ADMIN)
- Permission system with granular access control
- Secure password hashing with bcrypt (12 rounds)
- Rate limiting on auth endpoints
- Password reset flow with secure tokens
- Email verification system

Files added:
- lib/auth/: Core auth library (types, permissions, context, hooks, middleware)
- pages/api/auth/: Auth API routes (NextAuth, register, forgot-password, verify-email)
- pages/auth/: Auth pages (signin, signup, forgot-password, reset-password, verify-email)
- components/auth/: Reusable auth components (LoginForm, RegisterForm, AuthGuard, etc.)

Updated _app.tsx to include SessionProvider for auth state management.
2025-11-23 03:52:09 +00:00

142 lines
4.5 KiB
TypeScript

import { useState } from 'react'
interface PasswordResetFormProps {
token: string
onSuccess?: () => void
onError?: (error: string) => void
}
export function PasswordResetForm({ token, onSuccess, onError }: PasswordResetFormProps) {
const [password, setPassword] = useState('')
const [confirmPassword, setConfirmPassword] = useState('')
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const [success, setSuccess] = useState(false)
const validatePassword = (): string | null => {
if (password.length < 8) {
return 'Password must be at least 8 characters long'
}
const hasUpperCase = /[A-Z]/.test(password)
const hasLowerCase = /[a-z]/.test(password)
const hasNumbers = /\d/.test(password)
if (!hasUpperCase || !hasLowerCase || !hasNumbers) {
return 'Password must contain uppercase, lowercase, and numbers'
}
if (password !== confirmPassword) {
return 'Passwords do not match'
}
return null
}
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setIsLoading(true)
setError(null)
const validationError = validatePassword()
if (validationError) {
setError(validationError)
setIsLoading(false)
onError?.(validationError)
return
}
try {
const response = await fetch('/api/auth/reset-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token, password }),
})
const data = await response.json()
if (!response.ok) {
setError(data.message || 'An error occurred')
onError?.(data.message || 'An error occurred')
return
}
setSuccess(true)
onSuccess?.()
} catch (err) {
const errorMessage = 'An unexpected error occurred'
setError(errorMessage)
onError?.(errorMessage)
} finally {
setIsLoading(false)
}
}
if (success) {
return (
<div className="bg-green-50 border border-green-200 text-green-700 px-4 py-8 rounded-lg text-center">
<h3 className="text-lg font-medium mb-2">Password Reset Successful!</h3>
<p>Your password has been reset successfully.</p>
</div>
)
}
return (
<form className="space-y-6" onSubmit={handleSubmit}>
{error && (
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded relative text-sm">
{error}
</div>
)}
<div className="space-y-4">
<div>
<label htmlFor="new-password" className="block text-sm font-medium text-gray-700">
New Password
</label>
<input
id="new-password"
name="password"
type="password"
autoComplete="new-password"
required
value={password}
onChange={(e) => setPassword(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="At least 8 characters"
/>
<p className="mt-1 text-xs text-gray-500">
Must contain uppercase, lowercase, and numbers
</p>
</div>
<div>
<label htmlFor="confirm-new-password" className="block text-sm font-medium text-gray-700">
Confirm New Password
</label>
<input
id="confirm-new-password"
name="confirmPassword"
type="password"
autoComplete="new-password"
required
value={confirmPassword}
onChange={(e) => setConfirmPassword(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="Confirm your password"
/>
</div>
</div>
<button
type="submit"
disabled={isLoading}
className="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 disabled:opacity-50 disabled:cursor-not-allowed"
>
{isLoading ? 'Resetting...' : 'Reset password'}
</button>
</form>
)
}
export default PasswordResetForm