aith
aith2w ago

Better Auth with Convex working in dev but broken in prod

I am trying to get authentication to work with my convex + better auth set up. I've got the following envs in my dev and production deployments in Convex:
BETTER_AUTH_SECRET, SITE_URL
BETTER_AUTH_SECRET, SITE_URL
. In development, everything works fine however in production I am unable to login or sign up. I am able to create an account but in production it gives me this error:
Error

Uncaught Error: Unauthenticated
at getAuthUser [as getAuthUser] (../../node_modules/@convex-dev/better-auth/src/client/index.ts:538:13)
at async handler (../../convex/auth.ts:44:9)
Error

Uncaught Error: Unauthenticated
at getAuthUser [as getAuthUser] (../../node_modules/@convex-dev/better-auth/src/client/index.ts:538:13)
at async handler (../../convex/auth.ts:44:9)
My getCurrentUser method looks like this:
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
const user = await authComponent.getAuthUser(ctx);
return {
...user,
userProfiles: await ctx.db
.query('userProfiles')
.withIndex('by_userId', (q) => q.eq('userId', user?._id))
.collect()
};
}
});
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
const user = await authComponent.getAuthUser(ctx);
return {
...user,
userProfiles: await ctx.db
.query('userProfiles')
.withIndex('by_userId', (q) => q.eq('userId', user?._id))
.collect()
};
}
});
I would really appreciate some help here I've been trying for hours to resolve this and find out what is causing this because in production it is just not working. It's deployed on vercel
11 Replies
Convex Bot
Convex Bot2w 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!
Romi
Romi2w ago
This a brand new repo you working on?
aith
aithOP2w ago
Yeah
Romi
Romi2w ago
Are you able to share link or send code? need to see full picture as it will be much easier for me to help.
aith
aithOP2w ago
Yeah let me DM you the repo
Romi
Romi2w ago
ty
wes
wes2w ago
@aith i’m almost 100% sure you are just missing the correct env variable with the production url somewhere. either you have a cors issue with the cross domain plugin, or google auth isn’t being given the correct url in the google cloud console settings. lmk
aith
aithOP2w ago
All the envs were set up (and I'm only using email password no Oauth). The problem is somehow with the middleware. In production the redirect doesn't work and I haven't been able to figure out why yet
wes
wes2w ago
are you on next?
aith
aithOP2w ago
Yes
wes
wes2w ago
that’s always the other problem lol so hard to troubleshoot most of the time the only difference in prod is the env variables or the url somehow

Did you find this page helpful?