'Session returns null Get request however in Post returns the session correctly in Next.js in server side (nextAuth)

I am trying to get the session data such as id, name, etc on the server-side but returns null in the terminal. In the GET Method session is null, however, in POST method returns the session perfectly.

    import { getSession } from "next-auth/react";

export default async (req, res) => {
  const { method } = req;
  switch (method) {
    case "GET":
      try {
        const session = await getSession({ req });
        console.log(JSON.stringify(session));        //Returns null
        const jobs = await prisma.jobs.findMany();
        return res.status(200).json({ success: true, jobs });
      } catch (error) {
        return res.status(404).json({
          success: false,
          message: error.message,
        });
      }
}

case "POST":
  try {
    const session = await getSession({ req });
    console.log(JSON.stringify(session));   // here returns the session
    if (!session || session.role != 2) {
      return res.status(401).json({
        msg: "You are not authorized to perform this action",
      });
    }

[...nextauth].js ...nextauth.js file applying credentials provider, I am not sure if I have to implement anything else in addition

    import NextAuth from "next-auth";
import CredentialProvider from "next-auth/providers/credentials";
import axios from "axios";

export default NextAuth({
  providers: [
    CredentialProvider({
      name: "credentials",
      async authorize(credentials) {
        try {
          const user = await axios.post(
            `${process.env.API_URL}/auth/authentication/login`,
            {
              email: credentials.email,
              password: credentials.password,
            }
          );

          if (!user.data.user) {
            return null;
          }

          if (user.data.user) {
            return {
              id: user.data.user.id,
              name: user.data.user.name,
              surname: user.data.user.surname,
              email: user.data.user.email,
              role: user.data.user.role,
            };
          }
        } catch (error) {
          console.error(error);
        }
      },
    }),
  ],
  callbacks: {
    jwt: ({ token, user }) => {
      if (user) {
        token.id = user.id;
        token.name = user.name;
        token.surname = user.surname;
        token.email = user.email;
        token.role = user.role;
      }
      return token;
    },
    session: ({ session, token }) => {
      if (token) {
        session.id = token.id;
        session.name = token.name;
        session.surname = token.surname;
        session.email = token.email;
        session.role = token.role;
      }
      return session;
    },
  },
  secret: process.env.SECRET_KEY,
  jwt: {
    secret: process.env.SECRET_KEY,
    encryption: true,
    maxAge: 5 * 60 * 1000,
  },
  pages: {
    signIn: "/auth/login",
  },
});


Solution 1:[1]

This is weird. Can you try moving const session = await getSession({ req }) just before the switch statement?

...
const session = await getSession({ req });

console.log(JSON.stringify(session));

switch (method) {
  ...
}
....

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jdoroy