Skip to content

Commit

Permalink
enabled cors
Browse files Browse the repository at this point in the history
  • Loading branch information
aliirz committed Nov 13, 2024
1 parent 1056141 commit e0a5fef
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
console.log('Middleware running for path:', request.nextUrl.pathname)

// Handle CORS preflight requests
if (request.method === 'OPTIONS') {
const response = new NextResponse(null, { status: 200 })
response.headers.set('Access-Control-Allow-Origin', '*')
response.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization')
return response
}

// Create response early so we can add CORS headers to all responses
const response = NextResponse.next()
response.headers.set('Access-Control-Allow-Origin', '*')
response.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization')

// Add your public paths that should bypass auth
const publicPaths = [
'/images/', // Allow access to images
Expand All @@ -30,7 +45,7 @@ export function middleware(request: NextRequest) {

// If it's a public path, allow access
if (isPublicPath) {
return NextResponse.next()
return response
}

// For protected routes (including dashboard)
Expand All @@ -47,7 +62,7 @@ export function middleware(request: NextRequest) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}

return NextResponse.next()
return response
}

export const config = {
Expand Down

0 comments on commit e0a5fef

Please sign in to comment.