Mats
Mats5mo 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
BeBoRE5mo ago
Move your createInnerContext where you aren’t importing anything from Next Auth I guess
Tiago Freitas
Tiago Freitas2mo 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
Mats2mo 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 Freitas2mo ago
Can you share how you did it, is the above info enough? And have you tested the new trpc server actions?
Mats
Mats2mo 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
Cxso2mo ago
can you share the final code that worked pls ?
More Posts
tRPC failed on beehiiv.fetchBeehiivPosts: Invalid state: Controller is already closedIs there any solution to this error. I am getting while I try to hard refresh a page where I am callIs there a way to configure a trpc router to require an output() schema to be specified?I would like to require the output() schema to be specified on all the queries and mutations in my pErrors not caught in proceduresI have an application that uses Solid Start `@solidjs/start`. Most Solid Start apps use the `fetchResilent failure on client query?Hey friends, long time listener, first time caller 😉 I have encountered a quite confusing quirk thaAuth0 + fastify + tRPCHi, I've inherited a tRPC React/fastify app and need to migrate the auth from supertokens to auth0.Getting the type of contextIs it possible to get the type on a context object that is passed into a specific TRPC procedure aftIs it possible to perform attribute-based authorization after the .query?I have a TRPC method, `getStudentGradeById,` that accepts the `gradeId`. I want to return the grade How is inner context persistent if we call 'createContext' for every batch?As per title. According to the docs, inner context doesn't depend on the request, and is useful for does anyone know how put vs post calls map to the trpc procedures provided? (query vs mutation)I did check out the documentation but i only mentions GET and POST, not PUT . https://trpc.io/docs/Next.js. Migrating to turbopack casues context errorError: ``` react-dom.development.js:20662 Uncaught Error: Unable to retrieve application context.