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');
}

5 Replies
Hey, any luck on this? Help would be really appreciated
btw first you don't have to parse the response anymore you can use next-cookies plugin.
https://www.better-auth.com/docs/integrations/next#server-action-cookies
Next JS integration | Better Auth
Learn how to integrate Better Auth with Next.js
Thanks for the reply, but am using SvelteKit for this app
I actually think this is turso related issue. I remember seeing this error with turso.
Okay, thanks, I will ask in their discord server about this
Just found out the reason, it is related to cloudflare cpu time limit, this error returns when cf returns an html response when you exceed the cpu time limit.