Skip to content

Instantly share code, notes, and snippets.

@xxRockOnxx
Last active October 29, 2025 12:15
Show Gist options
  • Select an option

  • Save xxRockOnxx/7cf2e966859d9322039439d494d59b2d to your computer and use it in GitHub Desktop.

Select an option

Save xxRockOnxx/7cf2e966859d9322039439d494d59b2d to your computer and use it in GitHub Desktop.
Nuxt proxy to Laravel server route to avoid CORS and make sessions work
import { joinURL } from 'ufo'
// Full relative path: server/api/[...].ts
export default defineEventHandler((event) => {
const config = useRuntimeConfig(event)
const headers = new Headers()
headers.set('cookie', getRequestHeader(event, 'cookie')!)
// Read XSRF-TOKEN cookie set by Laravel
const xsrfToken = getCookie(event, 'XSRF-TOKEN')
// Send it back to Laravel as X-XSRF-TOKEN header
if (xsrfToken) {
headers.append('X-XSRF-TOKEN', xsrfToken)
}
const targetURL = joinURL(
// example: NUXT_PUBLIC_API_URL=http://my-laravel-app
config.public.apiUrl,
// remove `/api` prefix from Nuxt request.
// From: http://my-nuxt-app/api/proxy-this-to-laravel
// To: http://my-laravel-app/proxy-this-to-laravel
event.path.replace('/api', ''),
)
return proxyRequest(event, targetURL, {
headers: Object.fromEntries(headers),
fetchOptions: {
redirect: 'manual',
},
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment