Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] set.headers removes headers set by cors() #63

Closed
KryptXBSA opened this issue Jul 17, 2023 · 2 comments
Closed

[Bug] set.headers removes headers set by cors() #63

KryptXBSA opened this issue Jul 17, 2023 · 2 comments

Comments

@KryptXBSA
Copy link

KryptXBSA commented Jul 17, 2023

As the title says set.headers removes all the headers set by the @elysiajs/cors plugin.

@SaltyAom
Copy link
Member

I assume that you post this on Discord as well, so basically just use

set.headers['AnyHeader'] = 'Value'

instead of

set.headers = { AnyHeader: 'a' }

IMG_86AE59E2A266-1

@FrancoLab
Copy link

If you are still strugling, I managed to get this to work following this:

app.onAfterHandle(({ request, set }) => {
	if (!Bun.env.FRONTEND_URL) return false;

	set.headers["Access-Control-Allow-Origin"] = Bun.env.FRONTEND_URL;
	if (request.method === "OPTIONS") {
		set.headers["Access-Control-Allow-Headers"] =
			request.headers.get("Access-Control-Request-Headers") ??
			"Content-Type, Authorization";
		set.headers["Access-Control-Allow-Credentials"] = "true";
		set.headers["Access-Control-Allow-Methods"] =
			"GET, POST, DELETE, PUT, PATCH";
	}
});

app.use(
	cors({
		methods: ["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS"],
		origin: Bun.env.FRONTEND_URL,
		credentials: true,
		allowedHeaders: [
			"Content-Type",
			"Authorization",
			"Access-Control-Allow-Headers",
			"Access-Control-Allow-Credentials",
			"Access-Control-Request-Headers",
		],
	})
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants