el.xwx.moe/pages/api/routes/collections/getCollections.ts

41 lines
968 B
TypeScript
Raw Normal View History

2023-02-06 11:59:23 -06:00
import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "pages/api/auth/[...nextauth]";
2023-02-08 15:11:33 -06:00
import { prisma } from "@/lib/db";
2023-02-06 11:59:23 -06:00
type Data = {
2023-02-08 15:11:33 -06:00
response: object[] | string;
2023-02-06 11:59:23 -06:00
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
const session = await getServerSession(req, res, authOptions);
2023-02-08 15:11:33 -06:00
if (!session?.user?.email) {
return res.status(401).json({ response: "You must be logged in." });
2023-02-06 11:59:23 -06:00
}
2023-02-08 15:11:33 -06:00
const email: string = session.user.email;
2023-02-06 11:59:23 -06:00
2023-02-08 15:11:33 -06:00
const findCollection = await prisma.user.findFirst({
where: {
email: email,
},
include: {
collections: true,
2023-02-08 15:11:33 -06:00
},
});
const collections = findCollection?.collections.map((e) => {
return { id: e.id, name: e.name, createdAt: e.createdAt };
2023-02-08 15:11:33 -06:00
});
// console.log(session?.user?.email);
return res.status(200).json({
response: collections || [],
2023-02-06 11:59:23 -06:00
});
}