ruby on rails - OmniAuth::NoSessionError - You must provide a session to use OmniAuth. (configured in devise) -
hi learning how use omniauth backend ember app.
when run application below mentioned erroe omniauth::nosessionerror - must provide session use omniauth
on resue rails s applicataion halts @ line below.
172: def call!(env) # rubocop:disable cyclomaticcomplexity 173: unless env['rack.session'] 174: error = omniauth::nosessionerror.new('you must provide session use omniauth.') => 175: fail(error) 176: end 177:
config/intializer/devise
devise.setup |config| config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' require 'devise/orm/active_record' config.case_insensitive_keys = [ :email ] config.strip_whitespace_keys = [ :email ] config.http_authenticatable = true config.skip_session_storage = [:http_auth] config.stretches = rails.env.test? ? 1 : 10 config.reconfirmable = true config.expire_all_remember_me_on_sign_out = true config.password_length = 8..128 config.reset_password_within = 6.hours config.http_authenticatable_on_xhr = false config.navigational_formats = ['*/*', :html,:json] config.sign_out_via = :delete require 'omniauth-facebook' config.omniauth :facebook, env['8987087080'] , env['3d6a359a37c8780870dxxxx5'],:strategy_class => omniauth::strategies::facebook end
config/intializer/session_store.rb
rails.application.config.session_store :disabled
routes.rb
rails.application.routes.draw namespace :api namespace :v1 resources :users resources :games end end activeadmin.routes(self) #devise_for :admin_users, activeadmin::devise.config devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks', sessions: 'sessions' , registrations: "registrations", } devise_scope :user match 'users/sign_in', to: 'sessions#create', via: :post match 'api/v1/users' , to: 'registrations#create' , via: :post end end
gemfile.rb
source 'https://rubygems.org' # bundle edge rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.1.6' platforms :ruby # linux gem 'unicorn' gem 'foreman' gem 'delayed_job_active_record' end group :development, :test gem 'compass' gem 'pry' gem 'pry-remote' gem 'pry-rails' gem 'pry-rescue' gem 'pry-stack_explorer' gem 'pry-byebug' gem 'guard' gem 'guard-livereload' gem 'guard-rails' gem 'guard-rspec' gem 'guard-cucumber' gem 'guard-zeus' gem 'rspec-rails' end group :production #gem 'pg' end #authentication gem 'cancan' gem 'rolify' gem 'devise' gem 'omniauth' gem 'omniauth-facebook', '=1.4.0' gem 'oauth2' # use scss stylesheets gem 'sass-rails', '~> 4.0.0' gem 'bootstrap-sass', '~> 3.1.1' gem 'bootswatch-rails' # use uglifier compressor javascript assets gem 'uglifier', '>= 1.3.0' # see https://github.com/sstephenson/execjs#readme more supported runtimes # gem 'therubyracer', platforms: :ruby # use jquery javascript library gem 'jquery-rails' group :doc # bundle exec rake doc:rails generates api under doc/api. gem 'sdoc', require: false end #ember gem "active_model_serializers" gem "ember-source", "~>1.7.0" #asyc http calls gem 'hashie_rails' gem "typhoeus" gem "virtus" #middleware gem "rack-cors", require: 'rack/cors' platforms :mswin gem "wdm", :group => [:development, :test] end #gem 'wdm', '>= 0.1.0' # windows not include zoneinfo files, bundle tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin]
it because disabled session middleware (look @ output of rake middleware
). omniauth not work without session middleware.
you disabled here: rails.application.config.session_store :disabled
if trying ditch session because not use other omniauth, thing can write own middleware injects actiondispatch::session::cookiestore
, possibly other necessary middlewares based on url (i.e. if url /auth/*
). here example of use achieve (only uses session if url path not /api/...
):
# /config/application.rb config.middleware.insert_before actiondispatch::paramsparser, "selectivestack" # /config/initializers/omniauth.rb ::omniauthconfig = proc.new provider :github, # ... end # /app/middleware/selective_stack.rb class selectivestack def initialize(app) @app = app end def call(env) if env["path_info"].start_with?("/api/") # <--- change url path here @app.call(env) else middleware_stack.build(@app).call(env) end end private def middleware_stack @middleware_stack ||= begin actiondispatch::middlewarestack.new.tap |middleware| # needed omniauth middleware.use actiondispatch::cookies middleware.use rails.application.config.session_store, rails.application.config.session_options middleware.use omniauth::builder, &omniauthconfig # needed doorkeeper /oauth views middleware.use actiondispatch::flash end end end end
in example enable session middleware when url not start /api/
. will still need remove rails.application.config.session_store :disabled
, set session store, of course. in example use cookie store. might need tweak example based on middleware missing in rake middleware
. if you're not doing performance reasons reenable session middleware.
Comments
Post a Comment