2025-12-03 21:56:50 +00:00
|
|
|
import { useState } from 'react';
|
2025-12-13 02:34:34 +00:00
|
|
|
import { requestPasswordReset, resetPassword } from '../services/authService';
|
|
|
|
|
import type { ForgotPasswordFormData, ResetPasswordFormData } from '../types';
|
2025-12-03 21:56:50 +00:00
|
|
|
|
|
|
|
|
export function usePasswordReset() {
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const [error, setError] = useState<Error | null>(null);
|
|
|
|
|
const [success, setSuccess] = useState(false);
|
|
|
|
|
|
|
|
|
|
const handleRequestReset = async (data: ForgotPasswordFormData) => {
|
|
|
|
|
try {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
setSuccess(false);
|
|
|
|
|
await requestPasswordReset(data);
|
|
|
|
|
setSuccess(true);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(err as Error);
|
|
|
|
|
setSuccess(false);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleReset = async (data: ResetPasswordFormData) => {
|
|
|
|
|
try {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
setSuccess(false);
|
|
|
|
|
await resetPassword(data);
|
|
|
|
|
setSuccess(true);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(err as Error);
|
|
|
|
|
setSuccess(false);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
handleRequestReset,
|
|
|
|
|
handleReset,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
success,
|
|
|
|
|
};
|
|
|
|
|
}
|