Coffee11
Coffee117mo ago

Null user in convex function when using fetchQuery

Does ctx.auth only works with client components? I'm using clerkjs Clerkjs middleware
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';

const isPublicRoute = createRouteMatcher(['/sign-in(.*)', '/sign-up(.*)']);

export default clerkMiddleware(
(auth, request) => {
if (!isPublicRoute(request)) {
auth().protect();
}
},
{
debug: false,
}
);

export const config = {
matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
};
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';

const isPublicRoute = createRouteMatcher(['/sign-in(.*)', '/sign-up(.*)']);

export default clerkMiddleware(
(auth, request) => {
if (!isPublicRoute(request)) {
auth().protect();
}
},
{
debug: false,
}
);

export const config = {
matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
};
SSR
const convexUser = await fetchQuery(api.users.getUser, {
clerkId: clerkUserId!,
});
const convexUser = await fetchQuery(api.users.getUser, {
clerkId: clerkUserId!,
});
Convex function
export const getUser = query({
args: {
clerkId: v.string(),
},
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();

// This is null
console.log(identity);

return await ctx.db
.query('users')
.filter((q) => q.eq(q.field('clerkId'), args.clerkId))
.first();
},
});
export const getUser = query({
args: {
clerkId: v.string(),
},
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();

// This is null
console.log(identity);

return await ctx.db
.query('users')
.filter((q) => q.eq(q.field('clerkId'), args.clerkId))
.first();
},
});
2 Replies
erquhart
erquhart7mo ago
Next.js Server Rendering | Convex Developer Hub
Next.js automatically renders both Client and Server Components on the server
Coffee11
Coffee11OP7mo ago
@erquhart thank you so much

Did you find this page helpful?