kstulgys
kstulgys9mo ago

I use convex auth with nextjs setup, but userId always null

export const authQuery = customQuery(
query,
customCtx(async (ctx) => {
const userId = await auth.getUserId(ctx);
if (!userId) throw new Error("Authentication required");
const user = (await ctx.db.get(userId))!;
return { userId, user };
})
);
export const authQuery = customQuery(
query,
customCtx(async (ctx) => {
const userId = await auth.getUserId(ctx);
if (!userId) throw new Error("Authentication required");
const user = (await ctx.db.get(userId))!;
return { userId, user };
})
);
11 Replies
kstulgys
kstulgysOP9mo ago
In middleware this is true:
const isAuth = isAuthenticatedNextjs();
console.log({ isAuth });
const isAuth = isAuthenticatedNextjs();
console.log({ isAuth });
and when I'm redirected to authenticated page I get an error "Authentication required" bacause I call authQuery
Wingters
Wingters8mo ago
Were you able to find a fix for this issue? I am facing them same issue.
Oren
Oren8mo ago
how do you call the authQuery? if its from serverside you should pass token as the 3rd param
Wingters
Wingters8mo ago
Unsure, how he is doing but I am using fetchQuery. I am doing something like this:
const sermons = await fetchQuery(api.tasks.getTasks, {
token: convexAuthNextjsToken(),
});
const sermons = await fetchQuery(api.tasks.getTasks, {
token: convexAuthNextjsToken(),
});
Oren
Oren8mo ago
try
const sermons = await fetchQuery(api.tasks.getTasks, {}, {
token: convexAuthNextjsToken(),
});
const sermons = await fetchQuery(api.tasks.getTasks, {}, {
token: convexAuthNextjsToken(),
});
Wingters
Wingters8mo ago
Ah, nice! I am getting an error now. Progress! Server Error: Uncaught Error: Missing environment variable JWKS","trace":"Uncaught Error: Missing environment variable `JWKS Gotta figure this out, thanks!
Oren
Oren8mo ago
yeap make sure to set the auth env vars or run the automated script and it should work 👍
Wingters
Wingters8mo ago
automated script? Any docs you can point me to?
Oren
Oren8mo ago
Set Up Convex Auth - Convex Auth
Authentication library for your Convex backend
Manual Setup - Convex Auth
Authentication library for your Convex backend
Wingters
Wingters8mo ago
Thanks, Oren!
rgz
rgz8mo ago
Use this workaround: reactStrictMode: false, Add this to your nextjs.config.mjs file

Did you find this page helpful?