noob saibot
noob saibotβ€’5mo ago

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
import Resend from "@auth/core/providers/resend";
import { convexAuth } from "@convex-dev/auth/server";

export const { auth, signIn, signOut, store } = convexAuth({
providers: [
Resend({
apiKey: process.env.RESEND_API_KEY,
from: "noreply@myapp.com",
}),
],
});

"convex": "^1.13.2",
import Resend from "@auth/core/providers/resend";
import { convexAuth } from "@convex-dev/auth/server";

export const { auth, signIn, signOut, store } = convexAuth({
providers: [
Resend({
apiKey: process.env.RESEND_API_KEY,
from: "noreply@myapp.com",
}),
],
});

"convex": "^1.13.2",
Authorization - Convex Auth
Authentication library for your Convex backend
28 Replies
Michal Srb
Michal Srbβ€’5mo ago
Set Up Convex Auth - Convex Auth
Authentication library for your Convex backend
noob saibot
noob saibotOPβ€’5mo ago
@Michal Srb my nextjs app is indeed properly wrapped in a <ConvexAuthProvider> component. This is a snippet of my code:
...
const convex = new ConvexReactClient(env.NEXT_PUBLIC_CONVEX_URL);

export const ConvexAuthProviderWrapper = ({ children }: React.PropsWithChildren) => (
<ConvexAuthProvider client={convex}>
{children}
</ConvexAuthProvider>
);
...
const convex = new ConvexReactClient(env.NEXT_PUBLIC_CONVEX_URL);

export const ConvexAuthProviderWrapper = ({ children }: React.PropsWithChildren) => (
<ConvexAuthProvider client={convex}>
{children}
</ConvexAuthProvider>
);
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
Michal Srb
Michal Srbβ€’5mo ago
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
noob saibot
noob saibotOPβ€’5mo ago
@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
"@convex-dev/auth": "^0.0.47",
"convex": "^1.13.2",
"@convex-dev/auth": "^0.0.47",
"convex": "^1.13.2",
No description
noob saibot
noob saibotOPβ€’5mo ago
Also here is the @convex-dev folder in the node_modules folder:
No description
Devben
Devbenβ€’5mo ago
any update on this?
jamwt
jamwtβ€’5mo ago
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.
noob saibot
noob saibotOPβ€’5mo ago
@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.
jamwt
jamwtβ€’5mo ago
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.
noob saibot
noob saibotOPβ€’5mo ago
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:
No description
Devben
Devbenβ€’5mo ago
I just solve this error today
noob saibot
noob saibotOPβ€’5mo ago
Hi @Devben Could you share your solution? Thanks
Devben
Devbenβ€’5mo ago
Check you tsconfig "moduleResolution": "bundler", Make sure your moduleResolution is set to bundler I came across this in clerk docs
Devben
Devbenβ€’5mo ago
No description
noob saibot
noob saibotOPβ€’5mo ago
@Devben I set moduleResolution to "bundler". Unfortunately it has not solved my issue...
Devben
Devbenβ€’5mo ago
Can I see your tsconfig file?
Devben
Devbenβ€’5mo ago
No description
Devben
Devbenβ€’5mo ago
I face the same error earlier today But when I change the MR to bundler everything works fine Just provide your tsconfig.
noob saibot
noob saibotOPβ€’5mo ago
Here is my tsconfig:
{
"extends": "@paygroup/tsconfig/base.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"]
},
"plugins": [{ "name": "next" }],
"moduleResolution": "bundler",
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": [".", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
{
"extends": "@paygroup/tsconfig/base.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"]
},
"plugins": [{ "name": "next" }],
"moduleResolution": "bundler",
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": [".", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
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 relevance
Devben
Devbenβ€’5mo ago
I 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.
noob saibot
noob saibotOPβ€’5mo ago
I did all the changes, unfortunately the issue remains. I'll need a clear guidance from the team since the docs just mention:
import {
convexAuthNextjsMiddleware,
...
} from "@convex-dev/auth/nextjs/server"; // <-- this path!
import {
convexAuthNextjsMiddleware,
...
} from "@convex-dev/auth/nextjs/server"; // <-- this path!
Devben
Devbenβ€’5mo ago
@Michal Srb can you please take a look at this.
Jacob Kim
Jacob Kimβ€’5mo ago
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.
Oren
Orenβ€’5mo ago
^ 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
KΔ±te
KΔ±teβ€’5mo ago
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
noob saibot
noob saibotOPβ€’5mo ago
@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
Oren
Orenβ€’5mo ago
@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
Michal Srb
Michal Srbβ€’5mo ago
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?

Did you find this page helpful?