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
}
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." });
}
}
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
6 Replies
RockBacon
RockBacon10mo ago
I can't really tell without context of the app. Check the database permissions. e.g Check that the connection string has admin privileges. (once it is working/deployed in production you should reduce the privileges to least privilege)
cropsychooo
cropsychooo10mo ago
@rockbacon I will share the github repo, please ggive me a moment, I am literally so happy someone responded
cropsychooo
cropsychooo10mo ago
GitHub
GitHub - MarkoMalec/portfolio-nextjs
Contribute to MarkoMalec/portfolio-nextjs development by creating an account on GitHub.
RockBacon
RockBacon10mo ago
Code looks ok for a quick glance. Try changing your connection string to a planet scale instance. If that works then we know it is most likely your maria instance that could be the issue.
cropsychooo
cropsychooo10mo ago
Alright, let me try that.. by the way you have no idea how much I appercate this. Ill be back Could you though elaborate a bit more what does it mean to changing connection string to planet scale instance?
RockBacon
RockBacon10mo ago
Connection string looks something like this: psql "postgres://username:password@ep-little-darkness-79234402.eu-central-1.postgres.com:5432/db". exact same as a web address but instead of http you are using the respective db protocol As you would know you have this in your .env file. If you change this string/add one it allows you to connect to the database. Unlike a web address and exactly like ssh a connection string has a username and password (shown above). Exactly like in windows; Each account has different privileges, some have ones that only allow viewing some allow for full control e.g Admin. In large scale systems production would have a db account, developers would have their own account and different systems would have their own respective accounts. 2. Planetscale is just a provider for a mysql database, they don't have all the features of mysql (such as not allowing foreign keys this is due to how it scales with vitess) follow this guide to help yourself https://www.prisma.io/docs/guides/database/planetscale. You don't have to use planetscale but you permission issues seem to be due to you database. First maybe check if your U&Password in you connection string have the correct privileges
Want results from more Discord servers?
Add your server
More Posts