lucid
lucid3mo ago

convex auth - null user with fetchQuery on server

export const currentUser = query({
args: {},
handler: async (ctx) => {
const userIdentity = await ctx.auth.getUserIdentity();
console.log("server identity", await ctx.auth.getUserIdentity());
console.log("server id", await getAuthUserId(ctx));
const userId = await getAuthUserId(ctx);
if (userId === null) {
return null;
}
return await ctx.db.get(userId);
},
});
export const currentUser = query({
args: {},
handler: async (ctx) => {
const userIdentity = await ctx.auth.getUserIdentity();
console.log("server identity", await ctx.auth.getUserIdentity());
console.log("server id", await getAuthUserId(ctx));
const userId = await getAuthUserId(ctx);
if (userId === null) {
return null;
}
return await ctx.db.get(userId);
},
});
const user = await fetchQuery(api.user.queries.currentUser);
const user = await fetchQuery(api.user.queries.currentUser);
In the convex logs Im seeing a successful log of the user Id. However when Im calling the fetchQuery in a layout file, the user is null. If i call this exact same query using useQuery in a client component, I see a user returned successfully This is my middleware:
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
nextjsMiddlewareRedirect,
} from "@convex-dev/auth/nextjs/server";

const isSignInPage = createRouteMatcher(["/login"]);
const isProtectedRoute = createRouteMatcher(["/(.*)"]);

export default convexAuthNextjsMiddleware(async (request, { convexAuth }) => {
if (isSignInPage(request) && (await convexAuth.isAuthenticated())) {
return nextjsMiddlewareRedirect(request, "/");
}
if (
isProtectedRoute(request) &&
!(await convexAuth.isAuthenticated()) &&
!isSignInPage(request)
) {
return nextjsMiddlewareRedirect(request, "/login");
}
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
nextjsMiddlewareRedirect,
} from "@convex-dev/auth/nextjs/server";

const isSignInPage = createRouteMatcher(["/login"]);
const isProtectedRoute = createRouteMatcher(["/(.*)"]);

export default convexAuthNextjsMiddleware(async (request, { convexAuth }) => {
if (isSignInPage(request) && (await convexAuth.isAuthenticated())) {
return nextjsMiddlewareRedirect(request, "/");
}
if (
isProtectedRoute(request) &&
!(await convexAuth.isAuthenticated()) &&
!isSignInPage(request)
) {
return nextjsMiddlewareRedirect(request, "/login");
}
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
2 Replies
Convex Bot
Convex Bot3mo 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!
lucid
lucidOP3mo ago
Never mind, just found a solution. For anyone reading this and using Convex Auth wanting to get the user on server You need to add
{ token: await convexAuthNextjsToken() },
{ token: await convexAuthNextjsToken() },
as the 3rd param in the fetchQuery call from
import { convexAuthNextjsToken } from "@convex-dev/auth/nextjs/server";
import { convexAuthNextjsToken } from "@convex-dev/auth/nextjs/server";

Did you find this page helpful?