NextJS
Getserver Session Returning Null

Next Auth getServer() returning null

My application is already working for useSession (in client side), I decided to create e refactor a few pages to pass from server to component so I gain advantage in server side by using getServer() from nextjs. Even thought I was logged in I was receiving null from const session = getServer().

AI could not help me fast so searching the web I found in github community a tip to use getServerSession() instead, which was also returning null.

The secret was to import the authConfig.ts I configured according to documentation and my needs for the API (or Database), in my case there was no need to pass req, res as informed in github community. After that I could get all the data for my NextAuthProvider configured in beginning.

Check code below:

./src/app/pages/page.tsx
import { getServerSession } from "next-auth/next" // prefix get = server and prefox use = client
import { authConfig } from "@/app/lib/auth"; // Too many logics attached to your API or Dababase. Follow documentation or study this file
 
export default async function UserListPage(){
  const session = await getServerSession(authConfig); // getServerSession + authConfig fix problem
  console.log("session", session); // Your information
 
  //Work your server logic
  return(
    <div>
      {/* Add your Elements or Components */}
    </div>
  )
}

The configuration for authConfig was tough mission and a few things might be wrong. You must adapt your own API or Database. Anyway here follows my working fine file linked to Strapi API if want to check the reasoning and details.

See authConfig.ts file

Last updated on