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:
But I dont understand how is there no session when I use getSession...
this is the server file in pages
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
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)
@rockbacon I will share the github repo, please ggive me a moment, I am literally so happy someone responded
GitHub
GitHub - MarkoMalec/portfolio-nextjs
Contribute to MarkoMalec/portfolio-nextjs development by creating an account on GitHub.
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.
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?
Connection string looks something like this: psql "postgres://username:[email protected]: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