Getting OmniAuth :: NoSessionError Error with Rails 5 API

I created a new Rails 5 s app rails new appname --apithat seems great! I want to use it as a backend for an interface with React and eventually in a Chrome app. At the moment I want to create an API.

I used the following gems

  • gem 'omniauth'
  • gem 'omniauth-oauth2'
  • gem 'devise'
  • gem 'devise_token_auth', git: 'git: //github.com/lynndylanhurley/devise_token_auth.git'
  • gem 'omniauth-twitter'
  • gem 'omniauth-facebook'
  • gem 'omniauth-google-oauth2'

And I followed the directions on their Github here too to complete the setup: http://www.developingandrails.com/2015/02/api-authentication-with-devisetokenauth.html

And now, when I run the application, I get:

Started GET "/" for 14.144.15.10 at 2016-07-17 17:21:46 +0000
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
OmniAuth::NoSessionError (You must provide a session to use OmniAuth.):

I was looking for answers on Github and StackOverflow, but no one seems to have a solution.

The only thing that seems to “fix” the problem is this:

 # config/application.rb
 config.middleware.use Rack::Session::Cookie

But this “solution” gives me this error in the console:

SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
        This poses a security threat. It is strongly recommended that you
        provide a secret to prevent exploits that may be possible from crafted
        cookies. This will not be supported in future versions of Rack, and
        future versions will even invalidate your existing user cookies.

Please, help! Thank.

+4
source share
3 answers

Not quite sure, but something that worked for me in the project:

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

Unfortunately, omniauth requires rack.sessionsome data to be stored between the request by the provider and the callback request.

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

API Omniauth Rails :

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

While config.middleware.insert_afterworking for me, the same middleware was not loaded, so I had to insert something else instead to insert it after. I found a similar answer at fooobar.com/questions/201302 / ... and just added:

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

c application.rb.

+3
source

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


All Articles