Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ KEYCLOAK_ISSUER=
KEYCLOAK_CLIENT_ID=
KEYCLOAK_JWKS_URL=

# LIFECYCLE UI
LIFECYCLE_UI_URL='http://localhost:3000'
# CORS
ALLOWED_ORIGINS='https://example.com,https://staging.example.com'
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports = {
KEYCLOAK_ISSUER: process.env.KEYCLOAK_ISSUER,
KEYCLOAK_CLIENT_ID: process.env.KEYCLOAK_CLIENT_ID,
KEYCLOAK_JWKS_URL: process.env.KEYCLOAK_JWKS_URL,
LIFECYCLE_UI_URL: process.env.LIFECYCLE_UI_URL,
ALLOWED_ORIGINS: process.env.ALLOWED_ORIGINS,
},
typescript: {
ignoreBuildErrors: true,
Expand Down
17 changes: 11 additions & 6 deletions src/server/middlewares/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
import { NextResponse } from 'next/server';
import type { Middleware } from './chain';

const corsHeaders = {
'Access-Control-Allow-Origin': process.env.LIFECYCLE_UI_URL,
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
};
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(',').map((o) => o.trim()) ?? [];

export const corsMiddleware: Middleware = async (request, next) => {
const origin = request.headers.get('origin');
const isAllowedOrigin = origin && allowedOrigins.includes(origin);

const corsHeaders: Record<string, string> = {
'Access-Control-Allow-Origin': isAllowedOrigin ? origin : allowedOrigins[0],
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
};

if (request.method === 'OPTIONS') {
return new NextResponse(null, { status: 204, headers: corsHeaders });
}
Expand Down