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:
The error is caused by the createInnerTRPCContext function.
Has anyone experienced a similar issue or could provide insights into how to navigate this problem?6 Replies
Move your createInnerContext where you aren’t importing anything from Next Auth I guess
@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.
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.
Can you share how you did it, is the above info enough? And have you tested the new trpc server actions?
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?
can you share the final code that worked pls ?