aithA
Convex Community4mo ago
1 reply
aith

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);
        }
    };
Was this page helpful?