Skip to content

Global csrf token check #1634

Answered by kettanaito
mauriceoc asked this question in Q&A
Jun 7, 2023 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

Hi, @mauriceoc.

It should be enough to prepend a less specific request handler to capture all requests and implement that CRSF validation token in it:

// mocks/handlers.js
import { rest } from 'msw'

export const handlers = [
  rest.all('*', (req, res, ctx) => {
    // If the token is valid, proceed with the request.
    // Just returning undefined from the handler will instruct MSW
    // to fallthrough, looking for any other matching handlers.
    if (validateCrsfToken(req)) {
      return
    }

    // If the token validation failed, respond with a mock 401 response. 
    return res(ctx.status(401))
  }),
  // ...other handlers.
]

There are, of course, multiple ways of doing this in ca…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@mauriceoc
Comment options

@Toshinaki
Comment options

@kettanaito
Comment options

@Toshinaki
Comment options

Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants