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:
@@ -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.');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user