codalware-auth
Version:
Complete authentication system with enterprise security, attack protection, team workspaces, waitlist, billing, UI components, 2FA, and account recovery - production-ready in 5 minutes. Enhanced CLI with verification, rollback, and App Router scaffolding.
44 lines (35 loc) • 1.26 kB
text/typescript
import { useState } from 'react';
export const usePasswordChange = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
const changePassword = async (currentPassword: string, newPassword: string, confirmPassword: string) => {
setLoading(true);
setError(null);
setSuccess(false);
if (newPassword !== confirmPassword) {
setError('Passwords do not match');
setLoading(false);
return false;
}
try {
const res = await fetch('/api/user/change-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ currentPassword, newPassword }),
});
const data = await res.json();
if (!res.ok) {
throw new Error(data.message || 'Password change failed');
}
setSuccess(true);
return true;
} catch (e: unknown) {
setError(e instanceof Error ? e.message : 'Password change failed');
return false;
} finally {
setLoading(false);
}
};
return { changePassword, loading, error, success, clearError: () => setError(null) };
};