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

How to return dynamic origin? #52

Closed
bestvow opened this issue Aug 8, 2018 · 3 comments
Closed

How to return dynamic origin? #52

bestvow opened this issue Aug 8, 2018 · 3 comments

Comments

@bestvow
Copy link

bestvow commented Aug 8, 2018

String|Function(ctx)} origin Access-Control-Allow-Origin, default is request Origin header
what is Function(ctx) expect return value?
I tried
origin:ctx=>ctx.request.origin
It doesn't work.

@move-zig
Copy link

move-zig commented Aug 17, 2018

The function should return a string.

I'd also like to get some guidance on this function. What's the best practice? Say I have three domains I want to grant access to, "apple.example.com", "banana.example.com", and "cherry.example.com". I'm thinking something like this:

const app = new Koa();
app.use(cors({
  origin: (ctx) => {
    const validDomains = [ 'apple.example.com', 'banana.example.com', 'cherry.example.com' ];
    if (validDomains.indexOf(ctx.request.header.origin) !== -1) {
      return ctx.request.header.origin;
    }
    return validDomains[0]; // we can't return void, so let's return one of the valid domains
  },
});

Is this a good method? Is there a better way?

@bestvow
Copy link
Author

bestvow commented Aug 24, 2018

Thank a lot!

@andreyrd
Copy link

I believe you have to return the domain that was actually matched.

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