ERR_TOO_MANY_REDDIRECTS使用OmniAuth/Devise/Rails登录



我正试图按照以下说明将Facebook登录添加到我的Rails 4网站:

https://github.com/plataformatec/devise/wiki/OmniAuth:-概述

我目前正在使用Devise和Pundit进行授权和身份验证。尽管我已经尽我所能按照指示进行了操作,但我还是犯了一个错误。当我点击"使用Facebook登录"按钮时,会弹出一个窗口,询问电子邮件/密码,当我提交信息时,我会收到一个错误页面,上面写着:

[MyApp.com]页面不工作

[MyApp.com]重定向您的次数太多。

尝试:

  • 重新加载页面
  • 清除cookie ERR_TOO_MANY_DIRECTS

我似乎以某种方式引入了重定向循环,但我并不真正理解数据流,所以很难找到哪里出了问题。

这是我的路线。rb:

Rails.application.routes.draw do
  get 'home/index'
  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", sessions: "sessions" }
  resources :movies
  root 'home#index'
end

omniauth_callbacks_controller.rb:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  before_filter :authenticate_user, :except => [:new, :create, :destroy]
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
  def failure
    redirect_to root_path
  end
end

OmniAuth在config/initializers/device.rb:中的配置

config.omniauth :facebook, '[APP ID]', '[APP SECRET]', callback_url: "https://#{ENV['C9_HOSTNAME']}/users/auth/facebook",
  :client_options => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}

我的用户模型(user.rb):

class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, and :timeoutable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :omniauth_providers => [:facebook]
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
    end
  end
  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
      end
    end
  end
end

在我看来(使用HAML)的链接:

%button
  = link_to "Log in with Facebook", user_omniauth_authorize_path(:facebook)

我刚刚也遇到了这个问题!在你收到这个错误后,你能重新加载页面吗?它实际上已经让你登录了?如果是的话,那就是我所拥有的。登录成功了,但重定向不好。

您是否在登录后重定向页面的application_controller.rb中添加了函数after_sign_in_path_for(resource)?我有,这就是重定向发生的原因。

我的解决方案是修复if语句以包含正确的referrer。我刚刚在|| request.referer.include?("google")中添加了,当然你可能需要将其更改为引用url中的"facebook"帐户。

def after_sign_in_path_for(resource)
  sign_in_url = new_user_session_url
  if request.referer == sign_in_url || request.referer.include?("google")
    super
    goals_path
  else
    stored_location_for(resource) || request.referer || root_path
  end   
end

现在。。。如果你没有使用过那个函数,那么我的答案对你来说将毫无用处。祝你好运

最新更新