Signin throws an occasional not valid JSON error

Hey everyone, I have a signin server action which simply lets user signin , but sometimes it throws a 500 error saying "<", "!DOCTYPE" ... is not valid JSON, I dont know what might cause this, also it can be the (Turso) database error, but am not sure, Why does it throw error occasionally or only at certain times? sometimes its quite frequent and gives very bad UX. This is my signin code :

signin: async ({ request, cookies }) => { let form = await request.formData(); let email = form.get('email') as string; let password = form.get('password') as string; try { let res = await auth.api.signInEmail({ body: { email, password }, asResponse: true }); function getCookieValue(cookieString: string) { const match = cookieString.match(/better-auth\.session_token=([^;]+)/); return match ? decodeURIComponent(match[1]) : ''; } cookies.set('better-auth.session_token', getCookieValue(res.headers.getSetCookie()[0]), { path: '/', httpOnly: true, maxAge: 604800, sameSite: 'lax' }); } catch (err) { if (err instanceof APIError) { return fail(400, { errMessage: err.body.message as string }); } } redirect(303, '/profile'); }
WhatsApp_Image_2024-11-15_at_5.52.14_PM.jpeg
Was this page helpful?