From ca584a05a1a27dddf8f965ef6199470359738879 Mon Sep 17 00:00:00 2001 From: Jaume Alavedra Date: Wed, 7 Aug 2024 13:22:42 +0200 Subject: [PATCH] chore: add cors disabled middleware --- examples/apps/auth-sample/next.config.js | 21 ------------------- .../apps/auth-sample/src/pages/middleware.ts | 21 +++++++++++++++++++ 2 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 examples/apps/auth-sample/src/pages/middleware.ts diff --git a/examples/apps/auth-sample/next.config.js b/examples/apps/auth-sample/next.config.js index b5e19733..3dd7ef15 100644 --- a/examples/apps/auth-sample/next.config.js +++ b/examples/apps/auth-sample/next.config.js @@ -1,25 +1,4 @@ /** @type {import('next').NextConfig} */ module.exports = { reactStrictMode: true, - async headers() { - return [ - { - // matching all API routes - source: '/api/:path*', - headers: [ - {key: 'Access-Control-Allow-Credentials', value: 'true'}, - {key: 'Access-Control-Allow-Origin', value: '*'}, - { - key: 'Access-Control-Allow-Methods', - value: 'GET,OPTIONS,PATCH,DELETE,POST,PUT', - }, - { - key: 'Access-Control-Allow-Headers', - value: - 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Authorization', - }, - ], - }, - ]; - }, }; diff --git a/examples/apps/auth-sample/src/pages/middleware.ts b/examples/apps/auth-sample/src/pages/middleware.ts new file mode 100644 index 00000000..0b97b1da --- /dev/null +++ b/examples/apps/auth-sample/src/pages/middleware.ts @@ -0,0 +1,21 @@ +import { NextResponse } from 'next/server' +import type { NextRequest } from 'next/server' + +export function middleware(req: NextRequest) { + const res = NextResponse.next(); + + res.headers.set('Access-Control-Allow-Credentials', 'true'); + res.headers.set('Access-Control-Allow-Origin', '*'); + res.headers.set('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT'); + res.headers.set('Access-Control-Allow-Headers', 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Authorization'); + + if (req.method === 'OPTIONS') { + return new NextResponse(null, { status: 200, headers: res.headers }); + } + + return res; +} + +export const config = { + matcher: '/api/:path*', +} \ No newline at end of file