aith
aith2mo ago

Error handling with Convex Auth

Okay I don't know if this is something that's very obvious and I am missing it or something that's a limitation but I am implementing Convex Auth with Password provider. But the error handling seems not very elegant. For instance, I wanna catch incorrect password and not found email. It seems to me like I am supposed to just catch instances of "InvalidSecret" and "InvalidAccountId" but this seems not very clean. Or if my implementation is wrong I would love some advice (granted this is some AI slop code but still):
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError(null);
setIsLoading(true);

try {
await signIn('password', {
email,
password,
flow: 'signIn',
});

router.push('/dashboard');
} catch (err) {
if (err instanceof Error) {
if (err.message.includes('InvalidSecret')) {
setError('Incorrect password. Please try again.');
} else if (err.message.includes('InvalidAccountId')) {
setError('No account found with this email address.');
} else {
setError(
'Login failed. Please check your credentials and try again.',
);
}
} else {
setError('An unexpected error occurred. Please try again.');
}
} finally {
setIsLoading(false);
}
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError(null);
setIsLoading(true);

try {
await signIn('password', {
email,
password,
flow: 'signIn',
});

router.push('/dashboard');
} catch (err) {
if (err instanceof Error) {
if (err.message.includes('InvalidSecret')) {
setError('Incorrect password. Please try again.');
} else if (err.message.includes('InvalidAccountId')) {
setError('No account found with this email address.');
} else {
setError(
'Login failed. Please check your credentials and try again.',
);
}
} else {
setError('An unexpected error occurred. Please try again.');
}
} finally {
setIsLoading(false);
}
};
1 Reply
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!

Did you find this page helpful?