2023-02-24 11:32:28 -06:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { getServerSession } from "next-auth/next";
|
2023-10-22 23:28:39 -05:00
|
|
|
import { authOptions } from "@/pages/api/v1/auth/[...nextauth]";
|
2023-03-05 15:03:20 -06:00
|
|
|
import getLinks from "@/lib/api/controllers/links/getLinks";
|
2023-02-24 11:32:28 -06:00
|
|
|
import postLink from "@/lib/api/controllers/links/postLink";
|
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
2023-02-24 11:32:28 -06:00
|
|
|
const session = await getServerSession(req, res, authOptions);
|
|
|
|
|
2023-07-19 11:14:52 -05:00
|
|
|
if (!session?.user?.id) {
|
2023-02-24 11:32:28 -06:00
|
|
|
return res.status(401).json({ response: "You must be logged in." });
|
2023-07-15 21:15:43 -05:00
|
|
|
} else if (session?.user?.isSubscriber === false)
|
|
|
|
res.status(401).json({
|
|
|
|
response:
|
2023-07-20 17:23:57 -05:00
|
|
|
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
2023-07-15 21:15:43 -05:00
|
|
|
});
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (req.method === "GET") {
|
2023-06-15 07:39:30 -05:00
|
|
|
const links = await getLinks(session.user.id, req?.query?.body as string);
|
2023-03-28 02:31:50 -05:00
|
|
|
return res.status(links.status).json({ response: links.response });
|
|
|
|
} else if (req.method === "POST") {
|
|
|
|
const newlink = await postLink(req.body, session.user.id);
|
|
|
|
return res.status(newlink.status).json({
|
|
|
|
response: newlink.response,
|
|
|
|
});
|
|
|
|
}
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|