Convex auth: auth.getSessionId returns null
I have implemented the new
convex auth
lib in my nextjs
app. Now I'm attempting to retrieve the session id
or the logged in user
in my server function (query). Following the tutorial here https://labs.convex.dev/auth/authz#use-authentication-state-in-backend-functions, calling auth.getSessionId(ctx)
or auth.getUserId(ctx)
returns null.
The auth
object has been properly setup
Authorization - Convex Auth
Authentication library for your Convex backend
28 Replies
Did you setup Next.js middleware?
https://labs.convex.dev/auth/setup#set-up-the-react-provider
Set Up Convex Auth - Convex Auth
Authentication library for your Convex backend
@Michal Srb my nextjs app is indeed properly wrapped in a <ConvexAuthProvider> component. This is a snippet of my code:
When you're mentioning
nextjs middleware
, are you referring to the middleware described here https://labs.convex.dev/auth/authz/nextjs?
That middleware implementation seems to only handle redirection in case of accessing a secured route. Am I correct?Server-side authentication in Next.js - Convex Auth
Authentication library for your Convex backend
No, the middleware is required. You have to click on the Next.js tab in the last setup step:
https://labs.convex.dev/auth/setup#set-up-the-react-provider
and follow the instructions for app router with SSA
Set Up Convex Auth - Convex Auth
Authentication library for your Convex backend
@Michal Srb the documentation for setting up nextjs middleware mentions to import the method
convexAuthNextjsMiddleware
from @convex-dev/auth/nextjs/server
. But it looks like this path does not exists, only providers, react and server
. See below the screenshot of my import autocompletion
Also here is the
@convex-dev
folder in the node_modules
folder:any update on this?
this might require convex 1.14, which we just released! https://news.convex.dev/announcing-convex-1-14-3/
Convex News
Announcing Convex 1.14
Convex Auth for Next.js server side auth. We made some changes to make ConvexReactClient to enable Convex Auth for Next.js server side authentication.
@jamwt I've just upgraded to latest
convex: ^1.14.0
. Still I can't find the path @convex-dev/auth/nextjs/server
as mentioned in the documentation. As such, I am unable to setup middleware
in nextjs as mentioned in the docs.are you also on 0.0.50 now of @convex-dev/auth ? https://www.npmjs.com/package/@convex-dev/auth
npm
@convex-dev/auth
Authentication for Convex. Latest version: 0.0.50, last published: a day ago. Start using @convex-dev/auth in your project by running
npm i @convex-dev/auth
. There are no other projects in the npm registry using @convex-dev/auth.Ok, I have just upgraded to
0.0.50
. Now I can see the .../nextjs
path, but not the .../nextjs/server
path as mentioned in docs. Here is the screenshot of my autocompletion:I just solve this error today
Hi @Devben Could you share your solution? Thanks
Check you tsconfig "moduleResolution": "bundler",
Make sure your moduleResolution is set to bundler
I came across this in clerk docs
@Devben I set moduleResolution to "bundler". Unfortunately it has not solved my issue...
Can I see your tsconfig file?
I face the same error earlier today
But when I change the MR to bundler everything works fine
Just provide your tsconfig.
Here is my
tsconfig
:
It is worth noting that my application is a monorepo
(turborepo). The tsconfig.json
above is the configuration of the next
app within the monorepo, not sure if this info could be of any relevanceI don't know but can you just try mine maybe it may work this code will be under the compilerOption
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
That's what I did and my code start working again haha π but if this doesn't work then I think you will need assistance from expert since am new to this too.
I'll say delete your node_modules re-install every packages.
I did all the changes, unfortunately the issue remains. I'll need a clear guidance from the team since the docs just mention:
@Michal Srb can you please take a look at this.
I have the same issue. Even though I can get my auth token with
useAuthToken()
in React, convex websocket requests do not have auth token in their messages. ctx.auth.getUserIdentity()
in the server just returns null.^ having exactly same issue. token is present. isAuthenticatedNextjs is also working but no luck getting user details anyway its always null with ssr and on client
work around;
on the 4th step here https://labs.convex.dev/auth/setup#set-up-the-react-provider ->
nextjs tab -> "App Router with SSA" doesn't work but "App Router without SSA" works β
so its about the nextjs provider / ssr case only
I have this problem, but when I changed my auth,config.ts issuer to my convex.url.site istead of localhost it returns value now.
Im using App Router with SSA
@Oren what do you mean by
App Router without SSA
works? Could you indicate the exact place in the documentation you are refering to? Or could you, if it is possible, share some code snippet to see what is working in your case? Thanks a lot@noob saibot go to the link I shared and click on Next.js and then 2nd sub tab "App Router without SSA"
it worked but caused nextjs error on local, then I switched to auth.js integration instead following the stack post. That worked well in app but didn't work query/mutation authorizations so I removed my convex project and using pocketbase instead
Hey folks, I'm not sure all the issues mentioned here are related. Please open new thread in #support-community with as much detail as possible (ideally with a repo for us to repro the issue).
@noob saibot can you try deleting all your node_modules folders, reinstalling packages, and then get the error from running
tsc
on command line?