Getting error OmniAuth::NoSessionError with Rails 5 API

While config.middleware.insert_after worked for me, the same middleware was not loaded so I had to insert choose something else to insert it after. I found a similar answer in http://stackoverflow.com/questions/15342710/adding-cookie-session-store-back-to-rails-api-app and simply added:

config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore

in application.rb.


Unfortunately, omniauth requires rack.session presence to keep some data between the request to provider and the callback request.

https://github.com/omniauth/omniauth/blob/master/lib/omniauth/strategy.rb#L173

To Omniauth with Rails API needs to return a session to middleware stack:

config.middleware.insert_after ActiveRecord::Migration::CheckPending, ActionDispatch::Cookies
config.middleware.insert_after ActionDispatch::Cookies, ActionDispatch::Session::CookieStore

Not totally sure, but something that worked for me in a project is:

  #config/application.rb
  config.middleware.insert_after(ActiveRecord::QueryCache, ActionDispatch::Cookies)
  config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore)