3

晚上 - 虽然这在之前已经出现过问题,但没有一个给出的解决方案可以解决这个问题,并且相信我已经涵盖了大多数以前的解决方案。基本上,我单击“使用 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>

路线.rb:

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

omn​​iauth.callback 模型:

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

User.rb 模型:

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

omn​​iauth_callback 控制器:

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

设计.rb(片段):

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

秘密.yml:

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"] %>

我将我的 bash 配置文件设置如下:

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

4

0 回答 0