【问题标题】:Devise + omniauth routing problem on HerokuHeroku上的设计+omniauth路由问题
【发布时间】:2011-09-23 03:37:37
【问题描述】:

我在这个问题上花了好几个小时...我有一个设计和omniauth 设置,允许用户使用他们的Facebook 帐户登录。我正在使用以下 gem 版本:

Installing devise (1.4.7)
Installing oauth2 (0.4.1)
Installing oa-oauth (0.2.6)
Installing omniauth (0.2.6)

除其他外,在我的 Devise 初始化程序中拥有应用程序 ID 和密码:

config.omniauth :facebook, 'app_id', 'app_secret',
          {:scope => 'email, offline_access', :client_options => {:ssl => {:verify => false}}}

虽然我也尝试过但没有运气:

config.omniauth :facebook, 'app_id', 'app_secret',
          # {:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}

我的用户模型中有 :omniauthable 指令,我在路由文件中定义了自己的回调控制器:

devise_for :users, :controllers => { :omniauth_callbacks => 'user/authentications', :registrations => 'registrations' }

root :to => 'pages#home'

我还尝试将其添加到路由文件中,但没有成功:

get '/users/auth/:provider' => 'user/authentications#passthru'

这是我的身份验证控制器:

class AuthenticationsController < Devise::OmniauthCallbacksController
  def index
    @authentications = current_user.authentications if current_user
  end

  def facebook
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:notice] = "Signed in successfully."
      sign_in_and_redirect(:user, authentication.user)
    elsif current_user
      current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
      flash[:notice] = "Authentication successful."
      redirect_to authentications_url
    else
      user = User.new
      user.apply_omniauth(omniauth)
      if user.save
        flash[:notice] = "Signed in successfully."
        sign_in_and_redirect(:user, user)
      else
        session[:omniauth] = omniauth.except('extra')
        redirect_to new_user_registration_url
      end
    end
  end

  def passthru
    render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
  end

  def destroy
    @authentication = current_user.authentications.find(params[:id])
    @authentication.destroy
    flash[:notice] = "Successfully destroyed authentication."
    redirect_to authentications_url
  end
end

还有我的 Facebook 登录链接:

这一切都在本地运行良好,但如果我在我的应用部署到 Heroku 时尝试使用我的 Facebook 帐户登录,我会在我的日志中看到以下内容并被定向到 404 页面:

2011-09-23T03:26:31+00:00 app[web.1]: ActionController::RoutingError (uninitiali
zed constant User::AuthenticationsController):

我尝试重置我的数据库,但这也无济于事。

这是我从 Heroku 出发的路线:

        new_user_session GET    /users/sign_in(.:format)               {:action=
>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)               {:action=
>"create", :controller=>"devise/sessions"}
    destroy_user_session GET    /users/sign_out(.:format)              {:action=
>"destroy", :controller=>"devise/sessions"}
  user_omniauth_callback        /users/auth/:action/callback(.:format) {:action=
>/facebook/, :controller=>"user/authentications"}
           user_password POST   /users/password(.:format)              {:action=
>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)          {:action=
>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format)         {:action=
>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)              {:action=
>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)                {:action=
>"cancel", :controller=>"registrations"}
       user_registration POST   /users(.:format)                       {:action=
>"create", :controller=>"registrations"}
   new_user_registration GET    /users/sign_up(.:format)               {:action=
>"new", :controller=>"registrations"}
  edit_user_registration GET    /users/edit(.:format)                  {:action=
>"edit", :controller=>"registrations"}
                         PUT    /users(.:format)                       {:action=
>"update", :controller=>"registrations"}
                         DELETE /users(.:format)                       {:action=
>"destroy", :controller=>"registrations"}
                    root        /                                      {:control
ler=>"pages", :action=>"home"}

非常感谢任何帮助。如果您需要任何其他信息,请告诉我。我知道这是一件小事,但我觉得我已经尝试了上千种不同的方法,但没有任何效果。

【问题讨论】:

  • 您的“passthru”操作不是呈现 404.html。如果你正在做 get '/users/auth/:provider' => 'user/authentications#passthru'
  • 有,但我目前没有使用。

标签: ruby-on-rails facebook heroku devise omniauth


【解决方案1】:

我认为,您的 authenicatine 行应该使用 find_or_create。

authentication = Authentication.find_or_create_by_provider_and_uid(omniauth['provider'], omniauth['uid'])

【讨论】:

  • 查找后我正在检查身份验证是否为零。如果它是 nil,它会在我的控制器中的该方法的下方进一步创建。
【解决方案2】:

我改变了

devise_for :users, :controllers => { :omniauth_callbacks => 'user/authentications', :registrations => 'registrations' }

devise_for :users, :controllers => { :omniauth_callbacks => 'authentications', :registrations => 'registrations' }

它开始在本地和 Heroku 上运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2013-01-12
    • 1970-01-01
    相关资源
    最近更新 更多