Search code examples
curlnext.jsnext-auth

Next-Auth: getSession returns null


API request for the below code through a browser works fine i.e. returns {"success":true,"data":{"user": ...}} but using cURL or REST-Client VS Code extension return null i.e. {"success":true,"data":null}. Below is the code where I return the session object through /api/query endpoint:

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

export default async (req, res) => {
  const session = await getSession({ req });
  res.status(200).json({ success: true, data: session });
};

I tried getting the session using the endpoint /api/auth/session too. The endpoint returns the session using the browser but returns null using cURL. Here is the cURL request curl http://localhost:3000/api/auth/session.
What's the reason for the behavior?

P.S. : I am using Google OAuth for authentication.


Solution

  • Auth depends on cookies, your browser automatically sends the cookies. But when you use cURL cookies are not present, hence auth cannot authenticate. Next-Auth cookies