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." });
}
}