User Session in SSR (Next.js)

I have a separate backend in Nest.js, and I am using Next.js for the frontend. I want to fetch the user session and user details in SSR, but when I do authClient.getSession(), it returns {data: null}, but it works fine in client side. I have read other posts as well, but I didn't understand.
19 Replies
landon
landonOP4w ago
@Better Auth I have a separate backend in Nest.js, and I am using Next.js for the frontend. I want to fetch the user session and user details in SSR, but when I do authClient.getSession(), it returns {data: null}, but it works fine in client side.
Better Auth
Better Auth4w ago
When working on SSR with a separate backend, you’ll want to use the server-side session API rather than the client‐focused hook. On SSR you should call the server’s session retrieval method (typically via auth.api.getSession) and pass in the proper request headers so that your cookie (which contains the session token) is available on the backend [1]. If you try to use authClient.getSession (meant for client-side usage) in SSR, it might return { data: null } because it doesn’t update the client-side store and isn’t designed to extract cookies from server headers [1]. Also, make sure that your SSR request includes the necessary cookie headers from Next.js so the Nest.js backend can locate the session; misconfigured cookie caching or header propagation could also lead to a null session [5]. In summary: • Use auth.api.getSession in server-side code. • Ensure you pass the request’s headers (including cookies) to the method. • Double-check that your cookie settings (including any cookie cache settings) are in sync between your frontend and backend. Let me know if you need further clarification! Sources: [1] Basic Usage | Better Auth [5] auth.api.getSession always returns null if session.cookieCache is not set in the auth
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Basic Usage | Better Auth
Getting started with Better Auth
GitHub
auth.api.getSession always returns null if session.cookieCache is n...
Is this suited for github? Yes, this is suited for github To Reproduce Log in using authClient.signIn.social successfully Navigate into dashboard route Calls await auth.api.getSession results null ...
landon
landonOP4w ago
@Better Auth Should I initiate auth instance in my next.js app also? Because it is initiated in separate backend.
Better Auth
Better Auth4w ago
Hi there! You don’t need to create a full auth instance in your Next.js app if your backend is already handling authentication. Instead, you should create a client in your Next.js app (using the createAuthClient helper from better-auth/react) to communicate with your backend’s auth endpoints [1]. This client handles actions like signing in, signing out, and obtaining the session, keeping your frontend decoupled from the full auth logic. Sources: [1] Next.js integration | Better Auth
Next.js integration | Better Auth
Integrate Better Auth with Next.js.
landon
landonOP4w ago
But then how will I be able to access user session on server side of next js? @Better Auth I have a separate backend in Nest.js, and I am using Next.js for the frontend. I want to fetch the user session and user details in SSR, but when I do authClient.getSession(), it returns {data: null}, but it works fine in client side. Should I initiate auth instance in my next.js app also? Because it is initiated in separate backend.
Better Auth
Better Auth4w ago
You're encountering this because the client-side authClient (designed for CSR) doesn’t automatically expose the necessary session cookies or headers in your SSR context. In a server-side (SSR) environment, you need to use an auth instance that’s configured to read the incoming request headers (typically via nextCookies or a similar plugin) so that the session cookie can be parsed and validated [2], [13]. If your authentication logic is running on a separate Nest.js backend, you have two options: 1. Configure your Next.js SSR to call the dedicated backend’s session endpoint (making sure to forward the proper cookie/header information). This way, your Next.js app acts as a client to your Nest.js auth service.
2. Alternatively, instantiate a server-side auth instance in your Next.js app with the proper configuration (including plugins like nextCookies) so that you can use auth.api.getSession directly on the SSR side. This is common practice when you want to leverage Better Auth’s built-in SSR session handling [10], [13]. In short, if you want to fetch the session on the server side using Better Auth’s methods, it’s recommended to initialize (or import) an auth instance in your Next.js app that’s set up for SSR rather than relying solely on the client-side authClient. This configuration ensures that sessions and user details are correctly detected from the incoming request headers. Let me know if you need further clarification! Sources: [2] Basic Usage | Better Auth [10] Migrating from NextAuth.js to Better Auth | Better Auth [13] Next.js integration | Better Auth
Basic Usage | Better Auth
Getting started with Better Auth
Migrating from NextAuth.js to Better Auth | Better Auth
A step-by-step guide to transitioning from NextAuth.js to Better Auth.
landon
landonOP4w ago
@bekacru I need your help here.
begot
begot4w ago
@landon can you try this?
const session = await auth.api.getSession({
headers: await headers,
})
const session = await auth.api.getSession({
headers: await headers,
})
landon
landonOP4w ago
But this auth instance is present in separate backend server. Should I create one in next js as well? @begot
begot
begot4w ago
are you in a monorepo by chance?
landon
landonOP4w ago
Yes
begot
begot4w ago
your best bet is making a shared auth package then
begot
begot4w ago
No description
begot
begot4w ago
then you can import auth anywhere
landon
landonOP4w ago
Okay, apart from this, we don't have any solution, right? We cannot fetch it in SSR. And if we fetch on client side using auth client, we will have some flicker on UI. Just confirming because I am a beginner.
begot
begot4w ago
to my understanding, .getSession() uses cookies, it doesn't fetch the session so it won't be present on the server yes, it will have a loading state. using auth.api.getSession() will ensure there is no flicker
landon
landonOP4w ago
Okay, great, thanks a lot.
begot
begot4w ago
of course, feel free to dm or ask here if you have any issues with making an auth package
landon
landonOP4w ago
Thanks, I appreciate it.

Did you find this page helpful?