Mats
Mats8mo ago

Strange error when testing with Auth.js v5

Hi there, I just created a fresh Next.js project and added next-auth version 5 beta for authentication and tRPC for server logic. I have followed the setup documentation for both next-auth and tRPC. Setup Overview - Next.js Version: 14.1.4 - next-auth Version: 5.0.0-beta.15 Encountered Issue During integration tests when testing tRPC procedures that utilize next-auth for session handling, I'm getting this weird error: Error: Cannot find module '/path/to/project/node_modules/next/server' imported from /path/to/project/node_modules/next-auth/lib/env.js Did you mean to import next/server.js? vbnet This error happens when mocking the tRPC session context through the use of createInnerTRPCContext. Test Example Here's the test that triggers the error:
import { createInnerTRPCContext } from "./../server/api/trpc";

test("hello test", async () => {
const ctx = createInnerTRPCContext({
session: {
user: { id: "123", name: "John Doe" },
expires: "1",
},
});
const caller = createCaller(ctx);

const res = await caller.test.hello();
});
import { createInnerTRPCContext } from "./../server/api/trpc";

test("hello test", async () => {
const ctx = createInnerTRPCContext({
session: {
user: { id: "123", name: "John Doe" },
expires: "1",
},
});
const caller = createCaller(ctx);

const res = await caller.test.hello();
});
The error is caused by the createInnerTRPCContext function.
import { auth } from "@/app/api/auth/[...nextauth]/auth";

export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = await auth();

return createInnerTRPCContext({ session });
};
import { auth } from "@/app/api/auth/[...nextauth]/auth";

export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = await auth();

return createInnerTRPCContext({ session });
};
Has anyone experienced a similar issue or could provide insights into how to navigate this problem?
6 Replies
BeBoRE
BeBoRE8mo ago
Move your createInnerContext where you aren’t importing anything from Next Auth I guess
Tiago Freitas
Tiago Freitas5mo ago
@Mats did you solve this? I want to start using trpc with nextauth (currently v4 but I can upgrade) and trpc server actions and wondering if anyone has done it before.
Mats
Mats5mo ago
We did solve it and everything eventually worked. Would say Auth v5 worked ok, but the docs are often lacking, especially for connecting it with trpc etc. i guess we should expect this when its still beta. But yes we did manage to implement protected trpc procedures that require auth from auth js. Also, testing the protected trpc procedures was painful.
Tiago Freitas
Tiago Freitas5mo ago
Can you share how you did it, is the above info enough? And have you tested the new trpc server actions?
Mats
Mats5mo ago
Ah i had not heard of the new trpc server actions, so dont know about that. Do you have the same issue as i had above or is there something else?
Cxso
Cxso5mo ago
can you share the final code that worked pls ?