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

Could not assign string or regex value for origin #16

Closed
surtr1st opened this issue Oct 9, 2023 · 5 comments
Closed

Could not assign string or regex value for origin #16

surtr1st opened this issue Oct 9, 2023 · 5 comments

Comments

@surtr1st
Copy link

surtr1st commented Oct 9, 2023

Hi, how can I resolve this CORS error? When set as a string or regex, this will show an error that says the origin prop is an empty string.

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

But when I use other approaches like boolean values or customize logic with functions, it will cause:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is my current usage:

new Elysia()
  .use(
    cors({
      origin: ({ headers }) => {
        const origin = headers.get("origin");
        return origin === ALLOWED_ORIGIN;
      },
      allowedHeaders: ["Content-Type", "Accept", "Authorization"],
      methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
    }),
  )
  .group("/api", (app) => app.use(auth).use(user))
  .listen(3000);
@Kiyo5hi
Copy link

Kiyo5hi commented Oct 24, 2023

Dup: #7

@dgpt
Copy link

dgpt commented Apr 22, 2024

+1 I'm also seeing this and cannot seem to find an adequate resolution :/

@cannap
Copy link

cannap commented Jun 26, 2024

same

{ origin: ({ headers }) => {
const origin = headers.get('origin')
return origin === 'https://xxxx'
},
This works

@SaltyAom
Copy link
Member

SaltyAom commented Jul 9, 2024

Can you verify if this was fixed in the latest 1.0.4 patch?

@surtr1st
Copy link
Author

surtr1st commented Jul 9, 2024

Can you verify if this was fixed in the latest 1.0.4 patch?

Sorry, this has been quite a while. I need to re-adjust the version and fix some old code in the new version of Elysia.

But yes, the latest version of the cors plugin has resolved the problem.

Previously, I'd found the issue within my code, yet I wasn't sure if I was doing it the wrong way or if just the plugin was bugged.

My issue at that time was the hook I'm injected with; if I remember correctly, it worked when I removed all beforeHandle. The version of Elysia used was around 0.7.x, and the plugin was also around that.

However, after I tried the latest version of this plugin (specifically 1.0.4), I didn't need to remove all those hooks anymore, and it worked perfectly.

@surtr1st surtr1st closed this as completed Jul 9, 2024
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

5 participants