Search code examples
apiruby-on-rails-5middlewarerails-admin

How to create a custom middleware while using rails 5 in api-mode?


I'm trying to create a custom middleware in rails 5 in api-mode where for only routes that don't start with /api/... it will add back in

middleware.use ActionDispatch::Cookies
middleware.use ActionDispatch::Cookies::Session

This post shows how to do it in rails 4: OmniAuth::NoSessionError - You must provide a session to use OmniAuth. (configured in devise)

I'm trying to create a middleware like the accepted answer in the above post but when adding the

config.middleware.insert_before ActionDispatch::ParamsParser, "SelectiveStack"

to application.rb it throws an error saying it can't find SelectiveStack.

It also won't let me add a require or includes in the application.rb to access it, so I was wondering if anyone come across this or had any ideas?

I may just have the format wrong or have it in the wrong path.


Solution

  • It turns out that ActionDispatch::ParamsParser from the other stackoverflow post middleware doesn't actually exist and the wording of the error was a bit confusing.

    So basically to get it working you can either put your middleware before an actual existing middleware or just use

    config.middleware.use 
    

    instead of

    config.middleware.insert_before