feat: Enhance login error reporting with backend pre-validation and update signup to require email verification instead of auto-login.

This commit is contained in:
pradeepkumar
2025-12-22 13:02:17 +05:30
parent 060643fed8
commit 6c6129e8d1
3 changed files with 96 additions and 62 deletions

View File

@@ -18,19 +18,41 @@ export default function LoginPage() {
setLoading(true);
setError('');
const result = await signIn('credentials', {
email,
password,
redirect: false,
});
try {
// First, validate credentials with backend to get proper error messages
const validateRes = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
});
setLoading(false);
const validateData = await validateRes.json();
if (result?.error) {
setError('Invalid email or password');
} else {
router.push('/');
router.refresh();
if (!validateRes.ok) {
// Show the actual error message from backend
setError(validateData.message || 'Invalid email or password');
setLoading(false);
return;
}
// If validation passed, use NextAuth to create session
const result = await signIn('credentials', {
email,
password,
redirect: false,
});
setLoading(false);
if (result?.error) {
setError('Login failed. Please try again.');
} else {
router.push('/');
router.refresh();
}
} catch (err) {
setLoading(false);
setError('An error occurred. Please try again.');
}
};