OmniAuth::NoSessionError - 您必须提供会话才能使用 OmniAuth.(在设计中配置) [英] OmniAuth::NoSessionError - You must provide a session to use OmniAuth. (configured in devise)

查看:26
本文介绍了OmniAuth::NoSessionError - 您必须提供会话才能使用 OmniAuth.(在设计中配置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用 omniauth 作为 ember 应用程序的后端.

Hi I am learning how to use omniauth as backend for ember app.

当我运行我的应用程序时,我得到了下面提到的错误OmniAuth::NoSessionError - 您必须提供会话才能使用 OmniAuth

when i run my application I get the below mentioned erroe OmniAuth::NoSessionError - You must provide a session to use OmniAuth

在resue rails s我的应用程序停止在下面的行.

on resue rails s my applicataion halts at line below.

  172: def call!(env) # rubocop:disable CyclomaticComplexity
    173:   unless env['rack.session']
    174:     error = OmniAuth::NoSessionError.new('You must provide a session to use OmniAuth.')
 => 175:     fail(error)
    176:   end
    177: 

配置/初始化/设计

Devise.setup do |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

config/intializer/session_store.rb

Rails.application.config.session_store :disabled

routes.rb

Rails.application.routes.draw do

  namespace :api do
    namespace :v1 do
      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 do 
    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 do # linux
  gem 'unicorn'
  gem 'foreman'
  gem 'delayed_job_active_record'
end
group :development, :test do
    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 do
    #gem 'pg'
end

#authentication
gem 'cancan'
gem 'rolify'
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook', '=1.4.0'
gem 'oauth2'



# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
gem 'bootstrap-sass', '~> 3.1.1'
gem 'bootswatch-rails'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'



# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'


group :doc do
  # bundle exec rake doc:rails generates the 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 do 
  gem "wdm", :group => [:development, :test]
end
#gem 'wdm', '>= 0.1.0'

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin]

推荐答案

这是因为你禁用了 session 中间件(查看 rake middleware 的输出).如果没有会话中间件,Omniauth 将无法工作.

It is because you disabled the session middleware (look at the output of rake middleware). Omniauth will not work without the session middleware.

您在此处禁用了它:Rails.application.config.session_store :disabled

如果您因为除了 Omniauth 之外不使用它而试图放弃会话,那么您唯一能做的就是编写自己的中间件,注入 ActionDispatch::Session::CookieStore 以及其他可能基于 URL 的必要中间件(即,如果 URL 是 /auth/*).这是我用来实现此目的的示例(仅当 URL 路径不是 /api/... 时才使用会话):

If you are trying to ditch session because you do not use it other than for Omniauth, then the only thing you can do is write your own middleware that injects ActionDispatch::Session::CookieStore and possibly other necessary middlewares based on the URL (i.e. if the URL is /auth/*). Here is an example of what I use to achieve this (only uses session if URL path is not /api/...):

# /config/application.rb
config.middleware.insert_before ActionDispatch::ParamsParser, "SelectiveStack"

# /config/initializers/omniauth.rb
::OmniAuthConfig = Proc.new do
  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 do |middleware|
        # needed for OmniAuth
        middleware.use ActionDispatch::Cookies
        middleware.use Rails.application.config.session_store, Rails.application.config.session_options
        middleware.use OmniAuth::Builder, &OmniAuthConfig
        # needed for Doorkeeper /oauth views
        middleware.use ActionDispatch::Flash
      end
    end
  end
end

在这个例子中,我只在 URL not/api/ 开头时启用会话中间件.当然,您仍然需要删除 Rails.application.config.session_store :disabled 并正确设置会话存储.在我的示例中,我使用了 cookie 存储.您可能需要根据 rake middleware 中缺少的中间件来调整我的示例.但是,如果您不是出于性能原因这样做,那么只需重新启用会话中间件即可.

In this example I only enable the session middleware when the URL does not start with /api/. You will still need to remove Rails.application.config.session_store :disabled and properly set up your session store, of course. In my example I use the cookie store. You might need to tweak my example based on which middleware you are missing in rake middleware. But if you're not doing this for performance reasons then just reenable the session middleware.

这篇关于OmniAuth::NoSessionError - 您必须提供会话才能使用 OmniAuth.(在设计中配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆