Prisma and mySQL 'POST' method 401 Unauthorized

I have Prisma synced with mysql database (mariaDB).
I have a "dashboard" that I can access with auth through discord.
There I have a form that does POST (and prisma.[whatever].create()) method on submit, but I am getting a 401 error Unauthorized.

Just for reference, fetching data with GET and doing prisma.[whatever].findMany(); works normally
I also can access and see tables through localhost:3000/api/[whatever]

I am assuming that there is no session data available on request since I am getting this in terminal when I try to submit:

https://next-auth.js.org/errors#client_fetch_error undefined {
  error: {},
  url: 'http://localhost:3000/api/auth/session',
  message: undefined
}

But I dont understand how is there no session when I use getSession...

this is the server file in pages

import { prisma } from "~/server/db";
import { getSession } from "next-auth/react";
import { PrismaClient } from '@prisma/client';

export default async function handler(req, res) {
  const session = await getSession({ req });
  
  if (req.method === "POST") {
    if (!session) {
      console.log('no session')
      return res.status(401).json({ error: "Not authenticated" });
    }

    // Extract post data from request body
    const { title, content } = req.body;

    try {
      // Create a new post in the database
      const post = await prisma.post.create({
        data: {
          title,
          content,
          authorId: session.user.id, // session from next-auth
        },
      });

      return res.status(201).json(post);
    } catch (error) {
      return res.status(500).json({ error: "Failed to create post." });
    }
  }


Please, for the love of god, does anyone know where the problem is? I am going crazy by not finiding ways out of this.

Thank you
Was this page helpful?