How to enable csrf on node express for graphql and graphiql, for example. lusca?

Having a node express server with csrf lusca, and I want to enable csrf also for graphql and graphiql, but how to do it?

const csrf = require('lusca').csrf()
const graphqlExpress = require('graphql-server-express').graphqlExpress
const graphiqlExpress  = require('graphql-server-express').graphiqlExpress

server.use((req, res, next) => {
     csrf(req, res, next)
  }
})

server.use('/graphql', (req, res, next) => {
  graphqlExpress( () => {
    const query = req.query.query || req.body.query;
    return {
      schema,
      context: Object.assign({ user }, req.context),
      debug: false,
      formatError(e) { console.log(e) },
    }
  })(req, res, next)
})

server.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
}))
+4
source share

Source: https://habr.com/ru/post/1675333/


All Articles