fix(auth): use better-auth client for password reset
- Use authClient.forgetPassword() instead of raw fetch - Use authClient.resetPassword() instead of raw fetch - Fixes 404 error on /api/auth/forget-password
This commit is contained in:
parent
3cd2fc9674
commit
7f907660c8
2 changed files with 12 additions and 25 deletions
|
|
@ -5,6 +5,7 @@ import { Input } from '@tpmjs/ui/Input/Input';
|
|||
import { Label } from '@tpmjs/ui/Label/Label';
|
||||
import Link from 'next/link';
|
||||
import { useState } from 'react';
|
||||
import { authClient } from '~/lib/auth-client';
|
||||
|
||||
export default function ForgotPasswordPage() {
|
||||
const [email, setEmail] = useState('');
|
||||
|
|
@ -18,21 +19,13 @@ export default function ForgotPasswordPage() {
|
|||
setLoading(true);
|
||||
|
||||
try {
|
||||
// Call the password reset API endpoint
|
||||
const response = await fetch('/api/auth/forget-password', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email,
|
||||
redirectTo: `${window.location.origin}/reset-password`,
|
||||
}),
|
||||
const { error: resetError } = await authClient.forgetPassword({
|
||||
email,
|
||||
redirectTo: `${window.location.origin}/reset-password`,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const data = await response.json();
|
||||
setError(data.message || 'Failed to send reset email');
|
||||
if (resetError) {
|
||||
setError(resetError.message || 'Failed to send reset email');
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { Label } from '@tpmjs/ui/Label/Label';
|
|||
import Link from 'next/link';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
import { Suspense, useState } from 'react';
|
||||
import { authClient } from '~/lib/auth-client';
|
||||
|
||||
function ResetPasswordForm() {
|
||||
const searchParams = useSearchParams();
|
||||
|
|
@ -39,20 +40,13 @@ function ResetPasswordForm() {
|
|||
setLoading(true);
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/auth/reset-password', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
newPassword: password,
|
||||
}),
|
||||
const { error: resetError } = await authClient.resetPassword({
|
||||
newPassword: password,
|
||||
token,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const data = await response.json();
|
||||
setError(data.message || 'Failed to reset password');
|
||||
if (resetError) {
|
||||
setError(resetError.message || 'Failed to reset password');
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue