allow options as well

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-11-04 20:26:35 +01:00
parent 853e6adbe2
commit 3431958c79
8 changed files with 44 additions and 20 deletions

View File

@@ -1,3 +0,0 @@
export { default } from 'next-auth/middleware';
export const config = { matcher: ['/dashboard'] };

View File

@@ -0,0 +1,24 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
export const config = { matcher: ['/api/sdk/:path*'] };
export const cors = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, PUT, OPTIONS',
'Access-Control-Allow-Headers':
'Content-Type, Authorization, Mixan-Client-Id, Mixan-Client-Secret',
};
export function middleware(request: NextRequest) {
const response = NextResponse.next();
if (request.method === 'OPTIONS') {
return NextResponse.json({}, { headers: cors });
}
Object.entries(cors).forEach(([key, value]) => {
response.headers.append(key, value);
});
return response;
}

View File

@@ -10,6 +10,10 @@ interface Request extends NextApiRequest {
}
export default async function handler(req: Request, res: NextApiResponse) {
if (req.method == 'OPTIONS') {
return res.status(202).json({});
}
if (req.method !== 'POST') {
return handleError(res, createError(405, 'Method not allowed'));
}

View File

@@ -10,6 +10,10 @@ interface Request extends NextApiRequest {
}
export default async function handler(req: Request, res: NextApiResponse) {
if (req.method == 'OPTIONS') {
return res.status(202).json({});
}
if (req.method !== 'PUT') {
return handleError(res, createError(405, 'Method not allowed'));
}

View File

@@ -10,6 +10,10 @@ interface Request extends NextApiRequest {
}
export default async function handler(req: Request, res: NextApiResponse) {
if (req.method == 'OPTIONS') {
return res.status(202).json({});
}
if (req.method !== 'PUT') {
return handleError(res, createError(405, 'Method not allowed'));
}

View File

@@ -11,6 +11,10 @@ interface Request extends NextApiRequest {
}
export default async function handler(req: Request, res: NextApiResponse) {
if (req.method == 'OPTIONS') {
return res.status(202).json({});
}
if (req.method !== 'PUT' && req.method !== 'POST') {
return handleError(res, createError(405, 'Method not allowed'));
}

View File

@@ -12,6 +12,10 @@ interface Request extends NextApiRequest {
}
export default async function handler(req: Request, res: NextApiResponse) {
if (req.method == 'OPTIONS') {
return res.status(202).json({});
}
if (req.method !== 'POST') {
return handleError(res, createError(405, 'Method not allowed'));
}