Hilthon
Hilthon
CCConvex Community
Created by Hilthon on 1/4/2025 in #support-community
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 });
}
}
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?
6 replies