2025-11-25 02:34:52 +07:00
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
|
import { supabase } from '@imphnen-frontend-service/service';
|
|
|
|
|
import { useNavigate } from 'react-router';
|
|
|
|
|
import { toast } from 'sonner';
|
|
|
|
|
|
|
|
|
|
export default function ResetPasswordPage() {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
const [isValidToken, setIsValidToken] = useState(false);
|
|
|
|
|
|
2025-11-27 15:12:45 +07:00
|
|
|
// useEffect(() => {
|
|
|
|
|
// // Check if we have a valid session (from the reset link)
|
|
|
|
|
// supabase.auth.getSession().then(({ data: { session } }) => {
|
|
|
|
|
// if (session) {
|
|
|
|
|
// setIsValidToken(true);
|
|
|
|
|
// } else {
|
|
|
|
|
// toast.error('Invalid or expired reset link');
|
|
|
|
|
// setTimeout(() => navigate('/auth/forgot-password'), 2000);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// }, [navigate]);
|
2025-11-25 02:34:52 +07:00
|
|
|
|
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
if (password !== confirmPassword) {
|
|
|
|
|
toast.error('Passwords do not match');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (password.length < 6) {
|
|
|
|
|
toast.error('Password must be at least 6 characters');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
|
|
|
|
|
const { error } = await supabase.auth.updateUser({
|
2025-11-25 19:59:08 +07:00
|
|
|
password: password,
|
2025-11-25 02:34:52 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toast.success('Password updated successfully!');
|
|
|
|
|
|
|
|
|
|
// Sign out and redirect to login
|
|
|
|
|
await supabase.auth.signOut();
|
|
|
|
|
navigate('/auth/login');
|
|
|
|
|
} catch (err) {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.error('Failed to reset password:', err);
|
2025-11-25 02:34:52 +07:00
|
|
|
toast.error((err as Error).message || 'Failed to reset password');
|
|
|
|
|
} finally {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-27 15:12:45 +07:00
|
|
|
// if (!isValidToken) {
|
|
|
|
|
// return (
|
|
|
|
|
// <div className="flex justify-center items-center min-h-screen bg-gray-50 dark:bg-gray-950">
|
|
|
|
|
// <div className="text-center">
|
|
|
|
|
// <div className="inline-block animate-spin rounded-full h-12 w-12 border-b-2 border-primary-600 mb-4"></div>
|
|
|
|
|
// <p className="text-gray-600 dark:text-gray-400">
|
|
|
|
|
// Verifying reset link...
|
|
|
|
|
// </p>
|
|
|
|
|
// </div>
|
|
|
|
|
// </div>
|
|
|
|
|
// );
|
|
|
|
|
// }
|
2025-11-25 02:34:52 +07:00
|
|
|
|
|
|
|
|
return (
|
2025-11-27 15:12:45 +07:00
|
|
|
<div className="flex justify-center items-center min-h-screen bg-gray-50 dark:bg-gray-950 p-4">
|
|
|
|
|
<div className="bg-white dark:bg-gray-900 w-full max-w-md p-8 rounded-2xl shadow-lg border border-gray-200 dark:border-gray-700">
|
2025-11-25 02:34:52 +07:00
|
|
|
<div className="text-center mb-8">
|
2025-11-27 15:12:45 +07:00
|
|
|
<h2 className="text-3xl font-bold text-gray-900 dark:text-white mb-2">
|
2025-11-25 02:34:52 +07:00
|
|
|
Set New Password
|
|
|
|
|
</h2>
|
2025-11-27 15:12:45 +07:00
|
|
|
<p className="text-gray-600 dark:text-gray-400">
|
|
|
|
|
Enter your new password below
|
|
|
|
|
</p>
|
2025-11-25 02:34:52 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
|
|
|
<div>
|
2025-11-25 19:59:08 +07:00
|
|
|
<label
|
|
|
|
|
htmlFor="password"
|
2025-11-27 15:12:45 +07:00
|
|
|
className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"
|
2025-11-25 19:59:08 +07:00
|
|
|
>
|
2025-11-25 02:34:52 +07:00
|
|
|
New Password
|
|
|
|
|
</label>
|
|
|
|
|
<input
|
|
|
|
|
id="password"
|
|
|
|
|
type="password"
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
|
placeholder="••••••••"
|
|
|
|
|
disabled={isLoading}
|
2025-11-27 15:12:45 +07:00
|
|
|
className="w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent disabled:bg-gray-100 disabled:cursor-not-allowed bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
2025-11-25 02:34:52 +07:00
|
|
|
required
|
|
|
|
|
minLength={6}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
2025-11-25 19:59:08 +07:00
|
|
|
<label
|
|
|
|
|
htmlFor="confirmPassword"
|
2025-11-27 15:12:45 +07:00
|
|
|
className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"
|
2025-11-25 19:59:08 +07:00
|
|
|
>
|
2025-11-25 02:34:52 +07:00
|
|
|
Confirm New Password
|
|
|
|
|
</label>
|
|
|
|
|
<input
|
|
|
|
|
id="confirmPassword"
|
|
|
|
|
type="password"
|
|
|
|
|
value={confirmPassword}
|
|
|
|
|
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
|
|
|
placeholder="••••••••"
|
|
|
|
|
disabled={isLoading}
|
2025-11-27 15:12:45 +07:00
|
|
|
className="w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent disabled:bg-gray-100 disabled:cursor-not-allowed bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
2025-11-25 02:34:52 +07:00
|
|
|
required
|
|
|
|
|
minLength={6}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
className="w-full py-3 bg-primary-600 text-white rounded-lg font-semibold hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 disabled:bg-gray-400 disabled:cursor-not-allowed transition-colors"
|
|
|
|
|
>
|
|
|
|
|
{isLoading ? 'Updating...' : 'Update Password'}
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|