Hilthon
Hilthon2mo ago

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?
5 Replies
Convex Bot
Convex Bot2mo 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!
Hilthon
HilthonOP2mo ago
import GitHub from "@auth/core/providers/github";
import Google from "@auth/core/providers/google";
import { convexAuth } from "@convex-dev/auth/server";
import { Password } from "@convex-dev/auth/providers/Password";
import { getAuthSessionId } from "@convex-dev/auth/server";
import { ConvexError } from "convex/values";

import { query } from "./_generated/server";
import GitHub from "@auth/core/providers/github";
import Google from "@auth/core/providers/google";
import { convexAuth } from "@convex-dev/auth/server";
import { Password } from "@convex-dev/auth/providers/Password";
import { getAuthSessionId } from "@convex-dev/auth/server";
import { ConvexError } from "convex/values";

import { query } from "./_generated/server";
The imports in the auth.ts file with the getSession function
ballingt
ballingt2mo ago
You need to send the token in the fetchQuery https://docs.convex.dev/api/modules/nextjs#fetchquery like fetchQuery(api.foo.bar, {}, {token})
Module: nextjs | Convex Developer Hub
Helpers for integrating Convex into Next.js applications using server rendering.
ballingt
ballingt2mo ago
I wonder how we can make this more clear, did you copy this code from somewhere?
Hilthon
HilthonOP2mo ago
I was reading the https://labs.convex.dev/auth documentation, that's why I wasn't really finding anything. Thanks I managed to get it working.
Convex Auth - Convex Auth
Authentication library for your Convex backend

Did you find this page helpful?