Convex + better auth 400 code with no errors

Hey, I was just trying to setup better auth with convex, but for some reason I always get a weird 400 error with "Invalid body parameters" for the /api/auth/sign-in/social route. No matter what provider I use, it's always the same. All my .env variables are there and working
14 Replies
Ping
Ping2w ago
How are you calling the endpoint?
Zetax
ZetaxOP2w ago
await authClient.signIn.social({ provider: "discord" }); Sorry for the ping, but should I try anything else? Still haven't been able to figure out what's wrong. I am happy to provide my code and logs if needed
Ping
Ping2w ago
can you show me your auth config? The Invalid body parameters doesn't make sense since you provided the right values, so I suspect it might be caused by the Convex integration's implementation
Zetax
ZetaxOP2w ago
Sure, my auth config looks like this:
import { convexAdapter } from "@convex-dev/better-auth";
import { convex } from "@convex-dev/better-auth/plugins";
import { requireEnv } from "@convex-dev/better-auth/utils";
import { betterAuth } from "better-auth";
import { betterAuthComponent } from "../../convex/auth";
import { type GenericCtx } from "../../convex/_generated/server";

const siteUrl = requireEnv("SITE_URL");
const discordClientId = requireEnv("DISCORD_CLIENT_ID");
const discordClientSecret = requireEnv("DISCORD_CLIENT_SECRET");

export const createAuth = (ctx: GenericCtx) =>
betterAuth({
baseURL: siteUrl,
database: convexAdapter(ctx, betterAuthComponent),
socialProviders: {
discord: {
clientId: discordClientId,
clientSecret: discordClientSecret,
},
},
plugins: [
convex(),
],
});
import { convexAdapter } from "@convex-dev/better-auth";
import { convex } from "@convex-dev/better-auth/plugins";
import { requireEnv } from "@convex-dev/better-auth/utils";
import { betterAuth } from "better-auth";
import { betterAuthComponent } from "../../convex/auth";
import { type GenericCtx } from "../../convex/_generated/server";

const siteUrl = requireEnv("SITE_URL");
const discordClientId = requireEnv("DISCORD_CLIENT_ID");
const discordClientSecret = requireEnv("DISCORD_CLIENT_SECRET");

export const createAuth = (ctx: GenericCtx) =>
betterAuth({
baseURL: siteUrl,
database: convexAdapter(ctx, betterAuthComponent),
socialProviders: {
discord: {
clientId: discordClientId,
clientSecret: discordClientSecret,
},
},
plugins: [
convex(),
],
});
` No runtime errors on the NextJS side except for the 400 route log as well Nothing in Convex logs too
Ping
Ping2w ago
Can you show me the network panel in dev tools of this? Of it erroring
Zetax
ZetaxOP2w ago
No description
No description
No description
Zetax
ZetaxOP2w ago
I just copied their NextJS handler code and added some logging to see where the error occurs, I guess it's on their side then?
No description
Zetax
ZetaxOP7d ago
Then again, it might not be. In the convex api route, the better-auth.handler() is actually what returns the Invalid parameters body
const createAuthWithLogging = (ctx: any) => {
const auth = createAuth(ctx);
const originalHandler = auth.handler;

// Wrap the handler with logging
auth.handler = async (request: Request) => {
try {
const response = await originalHandler.call(auth, request)

//log json response
const responseBody = await response.clone().json()
console.log(`Response body:`, responseBody)

return response
} catch (error) {
console.error(`Auth error:`, error)
console.error(`Error stack:`, error instanceof Error ? error.stack : 'No stack trace')
throw error
}
}

return auth
}
const createAuthWithLogging = (ctx: any) => {
const auth = createAuth(ctx);
const originalHandler = auth.handler;

// Wrap the handler with logging
auth.handler = async (request: Request) => {
try {
const response = await originalHandler.call(auth, request)

//log json response
const responseBody = await response.clone().json()
console.log(`Response body:`, responseBody)

return response
} catch (error) {
console.error(`Auth error:`, error)
console.error(`Error stack:`, error instanceof Error ? error.stack : 'No stack trace')
throw error
}
}

return auth
}
No description
Ping
Ping7d ago
Oh, yeah that would explain it. Otherwise your code looks fine to me.
Ping
Ping7d ago
I recommend opening an issue on their github https://github.com/get-convex/better-auth
GitHub
GitHub - get-convex/better-auth: Convex + Better Auth 🔥
Convex + Better Auth 🔥. Contribute to get-convex/better-auth development by creating an account on GitHub.
Zetax
ZetaxOP7d ago
I have created an issue but will leave this open for now if that is fine with you
Ping
Ping7d ago
yeah no worries
Zetax
ZetaxOP7d ago
Fixed it! Followed an old thread on the convex discord that had the exact same problem on here. It was a node 20.09 issue. Upgrading to 24 fixed it. Really weird issue not gonna lie. I would have never found that solution without that thread, i'm one lucky mf
Zetax
ZetaxOP7d ago
But now I'm getting this without any application errors 😐 Probably a simple fix (let's hope)
No description

Did you find this page helpful?