Using Clerk's getAuth() helper causes withClerkMiddleware to be unrecognized
Hi there,
Recently, I was following Theo's T3 stack tutorial on YouTube. When attempting to use the Clerk
getAuth()
helper function in the trpc.ts
file of the T3 stack application, an error is thrown stating that the withClerkMiddleware
function must be added to the Next.js middleware.ts
file. Prior to using the getAuth()
helper function, Clerk was working fine and recognized that withClerkMiddleware
had already been added to the middleware.ts
file.
Steps to Reproduce:
1. Follow Theo's T3 stack tutorial on YouTube.
2. At timestamp 1:21:28 in the tutorial, introduce the Clerk getAuth()
helper function in the trpc.ts file
to retrieve the authentication state for a user based on the NextApiRequest
argument passed to it.
3. Run the application using npm run dev
command from the project's root directory .
4. Observe the following error message (screenshot attached):
❌ tRPC failed on <no-path>: You need to use "withClerkMiddleware" in your Next.js middleware file. You also need to make sure that your middleware matcher is configured correctly and matches this route or page. See https://clerk.com/docs/quickstarts/get-started-with-nextjsExpected Result: The
getAuth()
helper function should be able to retrieve the authentication state for a user without causing any errors.
Actual Result:
An error message is thrown stating that the withClerkMiddleware
function must be added to the middleware.ts
file, and to check that Clerk has been properly configured.
Fixes Attempted:
1. Deleted .next
and restarted Next.js dev server using npm run dev
command.
2. Reinstalled @clerk/nextjs
NPM package.
3. Reconfigured middleware.ts
from scratch based on example from Clerk's quickstart documentation for Next.js.
I'm kind of lost here and would appreciate any help/advice on how to fix the bug. Thanks!Theo - t3․gg
YouTube
T3 Stack Tutorial - FROM 0 TO PROD FOR $0 (Next.js, tRPC, TypeScrip...
I've never worked this hard on a video before. I really hope y'all can benefit from this 🙏
GITHUB REPO https://github.com/t3dotgg/chirp
DEPLOYED APP https://xn--uo8h.t3.gg/
GET A JACKET IF YOU'RE COOL LIKE THAT https://shop.t3.gg/
ALL MY VIDEOS ARE POSTED EARLY ON PATREON https://www.patreon.com/t3dotgg
Everything else (Twitch, Twitter, Discor...
Next.js Overview | Clerk
Clerk is the easiest way to add authentication and user management to your Next.js application
40 Replies
do u have middleware.ts
if so lets see whats in it @roshen
@elpupper yes, I created it based on the default
middleware.ts
template provided by Clerk. All I changed was removing the NextRequest
argument passed to the callback in withClerkMiddleware
(Theo did the same thing) because it wasn't being used. Here are the contents of my middleware.ts
file:
Use Clerk with Next.js | Clerk
Authentication and User management for the modern web
where did u put the middleware.ts file?
Relative to the project root, its path is
src/middleware.ts
what does ur _app.tsx look like
These are the contents of my
_app.tsx
file (relative file path: src/pages/_app.tsx
):
hm
how did u install trpc
the error is from trpc
I used the Create T3 App tool (
npm create t3-app@latest
) and made sure to select tRPC in the installation CLI menu. I was skeptical about my installation too initially, so I recreated the entire project from scratch and still reproduced the same error.what does ur [trpc].ts
look like
These are the contents of my
[trpc].ts
file (relative file path: src/pages/api/trpc/[trpc].ts
). They haven't been edited since I first initialized the project:
ok
and in ur server/api/trpc.ts
u should have this yeah?
this isn't the whole file
but if u go to const t
Yes, I have that exact function (was created automatically during project initialization)
?
Here's what my
createTRPCContext
function looks like. It isn't the same as yours because I haven't completed the tutorial (encountered the error at timestamp 1:21:28):
u should continue watching
i think he gets the same error maybe
but in package.json
Hm, I'll try watching further ahead, but I do remember him refreshing the app after making these changes, and the posts loaded without any errors
whats ur clerk/nextjs
in dependencies
for me its
Here's mine:
"@clerk/nextjs": "^4.16.4",
It auto-installed the latest stable version, I thinktry with ^4.11.14
just to see
Alright, I'll try it and get back to you
Just tried it and reinstalled
node_modules
, the same error is still showing upits time to bring the real boys in then
cause i have no clue 😭
@James Perkins check this one out
Lmao thanks for your help tho, I appreciate it @elpupper
tried my best. No problem
might have to wait for a while since he is probably sleeping
No problem
can you change your matcher to this
@James Perkins Wow, that worked! The error isn't showing up anymore, and the posts are loading and rendering as they should
Could you explain why the change in the regex was necessary?
👍 It's almost like I've seen this 100 times and it is documented.
The trpc routes are different then api routes, they use
trpc/example.hello
for example. It causes Next.js defaults to mess up, so we advise to be specific and state you want it to run on trpc routes and then make sure it runs on the home page.@James Perkins I see. From my understanding of the
config.matcher
property in middleware.ts
, it specifies the paths on which the middleware should run. The previous matcher RegEx (/((?!static|.*\\..*|_next|favicon.ico).*)
) was filtering out _next
, static
, favicon.ico
and public
but matching all other paths right? Wouldn't that mean it should also match tRPC routes? Is there a reason we need to specifically mention that we want it to run on tRPC routes?Yes because it breaks due to the period in there.
We say "include tRPC" exclude everything else
Alright understood, thank you for your help!
It's strange that the error doesn't occur on Theo's system during the tutorial though
Yup, I think since he dropped that video, I answer about 20-50 a week.
Also a lot has changed since he dropped that video in middleware (outside of Clerk)
Before we didn't need to include
"/"
Now we do.I see, I'll be sure to go read up on the new updates to Next.js middleware
Thank you again for your help
I can't seem to mark the solution though, the bot keeps failing
damn i was kinda close
good to know i didnt notice
I used this and still get this error:
tRPC failed on <no-path>: Clerk: getAuth() was called but Clerk can't detect usage of authMiddleware(). Please ensure the following:
- authMiddleware() is used in your Next.js Middleware.
- Your Middleware matcher is configured to match this route or page.
- If you are using the src directory, make sure the Middleware file is inside of it.
You probably need to talk to the Clerk team a lot has changed in 7 months and I don’t work there anymore
nooo james 😦
I got my own company now it’s all good 😀
Lets goo what is it?
going to have a look now! im happy for you