Omniauth,Devise,Rails,Facebook。错误:未找到。身份验证passthru

时间:2016-07-12 21:03:15

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

晚上 - 尽管此处已提出问题,但没有一个解决方案可以解决这个问题,并且相信我已经涵盖了大多数以前的解决方案。基本上,我点击“注册Facebook'链接并收到错误Not found. Authentication passthru.。我将在下面附上一些代码片段,但如果有人可以提供帮助,那将不胜感激。我检查了模型,控制器,devise.rb文件,视图,看不出任何问题:-(非常感谢。

布局:Application.html.erb



<!DOCTYPE html>
<html>
  <head>
    <title>YelpClone</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

    <!-- <%= stylesheet_link_tag "reset" %>
    <%= stylesheet_link_tag "styles" %> -->
  </head>

  <body>

    <p class="notice"><%= notice %></p>
    <p class="alert"><%= alert %></p>

    <% if user_signed_in? %>
      <%= link_to 'Sign out', destroy_user_session_path, method: :delete %>
    <% else %>
      <%= link_to 'Sign in', new_user_session_path %>
      <%= link_to 'Sign up', new_user_registration_path %>
      <%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path(:facebook) %>
    <% end %>

  
    <%= yield %>
  </body>
</html>
&#13;
&#13;
&#13;

routes.rb中:

&#13;
&#13;
Rails.application.routes.draw do
  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
  root to: "restaurants#index"
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  resources :restaurants do
    resources :reviews
  end

end
&#13;
&#13;
&#13;

omniauth.callback模型:

&#13;
&#13;
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  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
&#13;
&#13;
&#13;

User.rb型号:

&#13;
&#13;
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  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
end
&#13;
&#13;
&#13;

omniauth_callback控制器:

&#13;
&#13;
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  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
&#13;
&#13;
&#13;

Devise.rb(摘录):

&#13;
&#13;
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'],
  callback_url: "http://localhost:3000/users/auth/facebook/callback"
&#13;
&#13;
&#13;

secrets.yml:

&#13;
&#13;
development:
  secret_key_base: 8ee200d918d837f66a6d5e005160d879d9b33f28460b29993a9463fd0c928f20447b5294c76f35739fcd8f54fd8ef425a0e11d05f4567d9a3e964959f5d2b5a5
  facebook_app_id: <%= ENV["FACEBOOK_APP_ID"] %>
  facebook_app_secret: <%= ENV["FACEBOOK_APP_SECRET"] %>

test:
  secret_key_base: 1600213d6d312bae827c8a68fc847d11d58d441193e1ed9aec5f04f392bbff411d37a660cab40e089ab4423b2e4addc1dbe1bfb028daec2ca0eb1da4cdc0f884
  facebook_app_id: <%= ENV["FACEBOOK_APP_ID"] %>
  facebook_app_secret: <%= ENV["FACEBOOK_APP_SECRET"] %>

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  facebook_app_id: <%= ENV["FACEBOOK_APP_ID"] %>
  facebook_app_secret: <%= ENV["FACEBOOK_APP_SECRET"] %>
&#13;
&#13;
&#13;

我按如下方式设置我的bash配置文件:

&#13;
&#13;
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
export FACEBOOK_APP_SECRET="3*******"
export FACEBOOK_APP_ID="1*******"
&#13;
&#13;
&#13;

0 个答案:

没有答案