diff --git a/next.config.js b/next.config.js index 4a6bb16f..771f71d2 100644 --- a/next.config.js +++ b/next.config.js @@ -1,4 +1,5 @@ /** @type {import('next').NextConfig} */ +const websiteUrl = process.env.NODE_ENV === 'production' ? "https://encoach.com" : "http://localhost:3000"; const nextConfig = { reactStrictMode: true, output: "standalone", @@ -8,7 +9,7 @@ const nextConfig = { source: "/api/packages", headers: [ {key: "Access-Control-Allow-Credentials", value: "false"}, - {key: "Access-Control-Allow-Origin", value: "https://encoach.com"}, + {key: "Access-Control-Allow-Origin", value: websiteUrl}, { key: "Access-Control-Allow-Methods", value: "GET", @@ -19,6 +20,21 @@ const nextConfig = { }, ], }, + { + source: "/api/tickets", + headers: [ + {key: "Access-Control-Allow-Credentials", value: "false"}, + {key: "Access-Control-Allow-Origin", value: websiteUrl}, + { + key: "Access-Control-Allow-Methods", + value: "POST,OPTIONS", + }, + { + key: "Access-Control-Allow-Headers", + value: "Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date", + }, + ], + }, ]; }, }; diff --git a/src/pages/api/tickets/index.ts b/src/pages/api/tickets/index.ts index 009ed4b9..04f0b6b7 100644 --- a/src/pages/api/tickets/index.ts +++ b/src/pages/api/tickets/index.ts @@ -20,13 +20,25 @@ const db = getFirestore(app); export default withIronSessionApiRoute(handler, sessionOptions); async function handler(req: NextApiRequest, res: NextApiResponse) { + // due to integration with the homepage the POST request should be public + if (req.method === "POST") { + await post(req, res); + return; + } + + // specific logic for the preflight request + if (req.method === "OPTIONS") { + res.status(200).end(); + return; + } if (!req.session.user) { res.status(401).json({ ok: false }); return; } - if (req.method === "GET") await get(req, res); - if (req.method === "POST") await post(req, res); + if (req.method === "GET") { + await get(req, res); + } } async function get(req: NextApiRequest, res: NextApiResponse) { @@ -36,7 +48,7 @@ async function get(req: NextApiRequest, res: NextApiResponse) { snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data(), - })), + })) ); } @@ -61,7 +73,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { description: body.description, }, [body.reporter.email], - `Ticket ${id}: ${body.subject}`, + `Ticket ${id}: ${body.subject}` ); } catch (e) { console.log(e);