Getting Convex Auth to work with NextJS middleware
I want to integrate Convex auth with NextJS. I'm following the following docs:
https://labs.convex.dev/auth/authz/nextjs
It requires setting up a middleware using
convexAuthNextjsMiddleware
- but whenever I try to use it, I get the following error (attached image).
I digged a bit and it seems that the @convex-dev/auth/nextjs/server/index.js
(which is server-only file) is importing nextjs/client.js
which is client-only and imports React hooks, which most likely is triggering the error.
I wonder how is that supposed to work? Did I miss something? I don't understand why the middleware was bundled together with context provider in a single file and if someone even tested if this worked correctly.
I wanted to create an issue on GH, but first wanted to double check if I'm missing something obvious.Server-side authentication in Next.js - Convex Auth
Authentication library for your Convex backend
11 Replies
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!
Here's error:
We have working demos of this (specifically
npx create convex
and picking Next.js + Convex Auth). Can you share more of your middleware file / more of your surrounding code?I suppose you mean
npx convex create
? If so, that doesn't seem to exist as well, hmmI'd love to see working example with NextJS middleware
Next.js Quickstart | Convex Developer Hub
Add Convex to a Next.js project
thanks @sshader , I checked the example with auth and yes, it works.
Then I upgraded NextJS to v15 and React to v19 and the same issue appeared with the provided example
but since those are still in RC, I guess we shouldn't expect support for it anytime soon? I might try to wire up a PR that could potentially solve the issue, but I'm wondering if this would be looked at at all
Yep we haven't looked into supporting RCs for Next + React yet (focus currently is making sure Convex Auth works properly on the latest stable React + Next). I'll add that to our FAQ + issues list -- happy to take contributions here (https://github.com/get-convex/convex-auth)
GitHub
GitHub - get-convex/convex-auth: Library for built-in auth
Library for built-in auth. Contribute to get-convex/convex-auth development by creating an account on GitHub.
thanks! actually, I am digging deeper and this might seem to be not related to Convex at all.
I was sitting on RC version of NextJS and React19, but I switched to Canary (both NextJS and React) and this seems to be fixed here
so apologies for confusion, there's still quite a few moving parts when it comes to the upcoming version of NextJS
we've found that there always is 🙂