HilthonH
Convex Community13mo ago
5 replies
Hilthon

Using AuthJS, how to get current user in route.ts in NextJS.

export const getSession = query({
  args: {},
  handler: async (ctx) => {
    const sessionId = await getAuthSessionId(ctx);
    if (sessionId === null) {
      throw new ConvexError("Client is not authenticated!");
    }
    const session = await ctx.db.get(sessionId);

    return session;
  },
});


export async function POST(req: Request) {
  try {
    const authSession = await fetchQuery(api.auth.getSession);

    console.log("AUTH_INFO", { authSession });

    return new Response("", { status: 200 });
  }
}


Every time, the session is null even though I am logged in, how can I correctly fetch the user session?
Was this page helpful?