Skip to content

Commit

Permalink
Merge pull request #503 from keystonejs/user-configurable-cors
Browse files Browse the repository at this point in the history
Make CORS configurable by end users
  • Loading branch information
timleslie committed Nov 14, 2018
2 parents 3133e15 + 9136f6e commit a92a6f4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
26 changes: 26 additions & 0 deletions .changeset/93d02184/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"releases": [{ "name": "@voussoir/server", "type": "minor" }],
"dependents": [
{ "name": "@voussoir/test-utils", "type": "patch", "dependencies": ["@voussoir/server"] },
{
"name": "@voussoir/cypress-project-access-control",
"type": "patch",
"dependencies": ["@voussoir/test-utils", "@voussoir/server"]
},
{
"name": "@voussoir/cypress-project-basic",
"type": "patch",
"dependencies": ["@voussoir/test-utils", "@voussoir/server"]
},
{
"name": "@voussoir/cypress-project-login",
"type": "patch",
"dependencies": ["@voussoir/test-utils", "@voussoir/server"]
},
{
"name": "@voussoir/cypress-project-twitter-login",
"type": "patch",
"dependencies": ["@voussoir/server"]
}
]
}
1 change: 1 addition & 0 deletions .changeset/93d02184/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Makes CORS user configurable
8 changes: 7 additions & 1 deletion packages/server/WebServer/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ module.exports = function createGraphQLMiddleware(keystone, { apiPath, graphiqlP
}
},
});
server.applyMiddleware({ app, path: apiPath });
server.applyMiddleware({
app,
path: apiPath,
// Prevent ApolloServer from overriding Keystone's CORS configuration.
// https://www.apollographql.com/docs/apollo-server/api/apollo-server.html#ApolloServer-applyMiddleware
cors: false,
});
if (graphiqlPath) {
app.use(graphiqlPath, (req, res) => {
if (req.user && req.sessionID) {
Expand Down
9 changes: 3 additions & 6 deletions packages/server/WebServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ module.exports = class WebServer {
this.app.use(require('express-pino-logger')(this.config.pinoOptions));
}

this.app.use(
cors({
origin: true,
credentials: true,
})
);
if (this.config.cors) {
this.app.use(cors(this.config.cors));
}

if (this.config.authStrategy) {
// Setup the session as the very first thing.
Expand Down
1 change: 1 addition & 0 deletions packages/server/WebServer/initConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const defaultConfig = {
port: process.env.PORT || 3000,
apiPath: '/admin/api',
graphiqlPath: '/admin/graphiql',
cors: { origin: true, credentials: true },
};

const remapKeys = {
Expand Down

0 comments on commit a92a6f4

Please sign in to comment.