【问题标题】:Devise + OmniAuth (only GoogleOAuth) == session_path no method error设计 + OmniAuth(仅限 GoogleOAuth)== session_path 无方法错误
【发布时间】:2014-10-04 14:35:54
【问题描述】:

当我拥有 gem 的“was”版本时,这段代码确实有效。

  • oauth2 1.0.0(原为 0.9.3)
  • omniauth 1.2.2(原为 1.2.1)
  • omniauth-oauth2 1.2.0(原为 1.1.2)
  • omniauth-google-oauth2 0.2.5(原为 0.2.2)
  • 设计 3.2.4(原为 3.2.3)

我大量使用了这个:https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview#google-oauth2-example

我最近需要升级。

我正在尝试使用 google_oauth2 来验证用户的 gmail 地址。如果它与预先批准的用户列表匹配,那么我想将它们视为已授权,如果不是,则不是。这是一个非常基本的设置。

这是我的设置。

app/models/user.rb ```

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :omniauthable, :omniauth_providers => [:google_oauth2]

```

配置/路由.rb ```

devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks" }
devise_scope :user do
    get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
    get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end

app/controllers/application_controller.rb

  class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  def new_session_path(scope)
      new_user_session_path
  end
end

```

我得到的错误是:

Showing /private/var/www/concerto-signage/vendor/bundle/ruby/2.1.0/gems/devise-3.2.4/app/views/devise/sessions/new.html.erb where line #3 raised:

undefined method `session_path' for #<#<Class:0x007fa2e4ae54f0>:0x007fa2e4aefea0>
Extracted source (around line #3):
1
2
3
4
5
6

  <h2>Sign in</h2>

  <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
    <div><%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %></div>

我不明白的是,为什么设计试图去那个观点?我只是希望它创建 current_user 并从那里获取它?

另外请注意,我什至没有包含 omniauth_callbacks 控制器,因为此时它甚至都不会触发(基于我所做的一些调试语句)

我在查看日志时注意到了这一点:

Started GET "/users/auth/google_oauth2/callback?state=029f48fd5d35243b095c6d6933c7699d6501f104a47b23a0&code=4/6SKMFVh
dZLHRRD-2GiIsyNXLNh-t.AhDB-ii59RcVsjMf6whcw_4NFnKMjwI" for 127.0.0.1 at 2014-08-11 13:56:52 -0400
Processing by OmniauthCallbacksController#failure as HTML

【问题讨论】:

  • 注意:stackoverflow.com/questions/24420942/…,但没有帮助
  • 将 'omniauth-oauth2' 降级回“1.1.2”(并因此将 oauth2 降级为 0.9.4”)解决了这个问题.. 一定是他们的问题。

标签: ruby-on-rails oauth devise omniauth google-oauth


【解决方案1】:

经过一番排查,终于搞明白了。

在 config/initializers/devise.rb 中,我必须更改我的 config.omniauth 调用以包含策略类。

  config.omniauth :google_oauth2,
                  'APP_ID',
                  'APP_SECRET',
                  :strategy_class => OmniAuth::Strategies::GoogleOauth2

【讨论】:

  • 如何动态获取策略类?我有几个提供者,但是使用 case 语句有点难看:/
猜你喜欢
  • 2014-09-12
  • 2018-10-19
  • 1970-01-01
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
相关资源
最近更新 更多