Skip to content
Snippets Groups Projects
Commit 6232cb5f authored by Yannis Barlas's avatar Yannis Barlas
Browse files

feat(server): pass cors config to apollo server

parent f17e633e
No related branches found
No related tags found
2 merge requests!52Docx,!17Graphql api
...@@ -20,6 +20,7 @@ const api = require('pubsweet-server/src/routes/api') ...@@ -20,6 +20,7 @@ const api = require('pubsweet-server/src/routes/api')
const index = require('pubsweet-server/src/routes/index') const index = require('pubsweet-server/src/routes/index')
const healthcheck = require('./healthcheck') const healthcheck = require('./healthcheck')
const createCORSConfig = require('./corsConfig')
const configureApp = app => { const configureApp = app => {
const models = require('@pubsweet/models') const models = require('@pubsweet/models')
...@@ -61,16 +62,8 @@ const configureApp = app => { ...@@ -61,16 +62,8 @@ const configureApp = app => {
} }
// Allow CORS from client if host / port is different // Allow CORS from client if host / port is different
if (config.has('pubsweet-client.url')) { const CORSConfig = createCORSConfig()
const clientUrl = config.has('clientUrl') && config.get('clientUrl') app.use(cors(CORSConfig))
app.use(
cors({
origin: clientUrl,
credentials: true,
}),
)
}
// Register passport authentication strategies // Register passport authentication strategies
app.use(passport.initialize()) app.use(passport.initialize())
......
const config = require('config')
const createCORSConfig = () => {
if (!config.has('pubsweet-client.url')) return null
const clientUrl = config.has('clientUrl') && config.get('clientUrl')
return {
origin: clientUrl,
credentials: true,
}
}
module.exports = createCORSConfig
const { graphqlUploadExpress } = require('graphql-upload') const { graphqlUploadExpress } = require('graphql-upload')
const createGraphQLServer = require('./graphqlServer') const createGraphQLServer = require('./graphqlServer')
const createCORSConfig = require('./corsConfig')
const api = app => { const api = app => {
app.use( app.use(
...@@ -13,7 +14,9 @@ const api = app => { ...@@ -13,7 +14,9 @@ const api = app => {
app.use(graphqlUploadExpress()) app.use(graphqlUploadExpress())
const server = createGraphQLServer() const server = createGraphQLServer()
server.applyMiddleware({ app }) const CORSConfig = createCORSConfig()
server.applyMiddleware({ app, cors: CORSConfig })
} }
module.exports = api module.exports = api
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment