#convex auth - null user with fetchQuery on server

2 messages · Page 1 of 1 (latest)

minor kelp
#
    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);


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)(.)"],
};

velvet lilyBOT
#

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](https://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!