You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am encountering an unauthorized error when attempting to create a new post, even though I am logged in via GitHub. Upon modifying the code in pages/api/post/index.ts to log the session, I observed the following error:
{
"message": "you are not authorized",
"error": {
"name": "PrismaClientValidationError",
"clientVersion": "5.7.0"
},
"data": null
}
Modified code to observe the issue
// POST /api/post
// Required fields in body: title
// Optional fields in body: content
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const { title, content } = req.body;
const session = await getSession({ req });
console.log('POST Session: ', session);
try {
const result = await prisma.post.create({
data: {
title: title,
content: content,
// author: { connect: { email: session?.user?.email } }, // to by pass the session auth
author: { connect: { email: "mypersonal@email.com"} },
},
});
res.status(201).send({ message: 'post created successfully', error: null, data: result});
} catch (err) {
res.status(401).send({ message: 'you are not authorized', error: err, data: null});
}
}
on the other hand when I delete a post and log session then I get user information there.
whereas if I do console.log('POST Session: ', session); above I get null
Description
I am encountering an unauthorized error when attempting to create a new post, even though I am logged in via GitHub. Upon modifying the code in
pages/api/post/index.ts
to log the session, I observed the following error:Modified code to observe the issue
on the other hand when I delete a post and log session then I get user information there.
whereas if I do
console.log('POST Session: ', session);
above I get nullNode version: v21.3.0
NPM version: 10.2.4
npm list next:
├─┬ next-auth@4.24.5
│ └── next@13.5.6 deduped
├─┬ next-superjson@0.0.4
│ ├─┬ babel-plugin-superjson-next@0.4.5
│ │ └── next@13.5.6 deduped
│ └── next@13.5.6 deduped
└── next@13.5.6
The text was updated successfully, but these errors were encountered: