• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License
    • Building Features
    • Commands
    • Code Style
    • Security Rules
    • Translations and Locales
    • Sending Emails
    • Writing API Routes
    • Validating API payload with Zod
    • Logging
    • Enable CORS
    • Encrypting Secrets
    • User Roles

Enable CORS | Next.js Firebase SaaS Kit

Enabling CORS is required if you want to allow serving HTTP request to external clients. For example, if you want to expose an API to some consumers: JS libraries, headless clients, and so on.

To enable CORS, you can use the with-cors middleware in two different ways.

You can simply call it in your handler:

tsx
import withCors from '~/core/middleware/with-cors';
function apiHandler(
req: NextApiRequest,
res: NextApiResponse
) {
withCors(res);
// your logic
}
export default apiHandler;

Alternatively, you can use it with withPipe:

tsx
import withCors from '~/core/middleware/with-cors';
function handler(
req: NextApiRequest,
res: NextApiResponse
) {
// your logic
}
export default withPipe(
withCors,
apiHandler
);

Additionally, you need to respond to OPTIONS request appropriately.

tsx
export function handler(
req: NextApiRequest,
res: NextApiResponse
) {
withCors(res);
if (req.method === `OPTIONS`) {
// add the method you want to allow
res.setHeader('Access-Control-Allow-Methods', 'GET');
return res.end();
}
}