Ruby on rails Omniauth未响应GET请求

Ruby on rails Omniauth未响应GET请求,ruby-on-rails,devise,omniauth,Ruby On Rails,Devise,Omniauth,我试图让用户能够选择通过Facebook登录 我的身份验证由Desive管理,我正在使用Omniauth Facebook gem 当我尝试在我的Desive视图(session/new.html.erb)中通过Facebook登录用户时,用户已成功登录 new.html.erb <div><%= link_to image_tag('facebook_login.png'), user_omniauth_authorize_path(:facebook) %></

我试图让用户能够选择通过Facebook登录

我的身份验证由Desive管理,我正在使用Omniauth Facebook gem

当我尝试在我的Desive视图(session/new.html.erb)中通过Facebook登录用户时,用户已成功登录

new.html.erb

<div><%= link_to image_tag('facebook_login.png'), user_omniauth_authorize_path(:facebook) %></div>
//add this for tell devise the omniauth configuration for facebook
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET']
//your secrets for development, is useful have it this way because you can use different applications for your rails enviroments
ENV['FACEBOOK_APP_ID'] = 'xxxxxxxxxxx';
ENV['FACEBOOK_SECRET'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//override the controller for omniauth callbacks, we will define this later, this is our custom behavior for dealing with what omniauth hash will return to us
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
def self.from_omniauth(auth)
  where(auth.slice(:provider, :uid)).first_or_create do |user|
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
  end
end

def self.new_with_session(params, session)
  if session["devise.user_attributes"]
    new(session["devise.user_attributes"], without_protection: true) do |user|
      user.attributes = params
      user.valid?
    end
  else
    super
  end
end

def password_required?
  super && self.provider.blank?
end

def update_with_password(params, *options)
  if encrypted_password.blank?
    update_attributes(params, *options)
  else
    super
  end
end

def has_no_password?
  self.encrypted_password.blank?
end

Desive是否只允许用户通过单个控制器登录facebook,或者我是否需要对链接路径进行其他更改?

我认为您可以将omniauth与Desive分开配置(在omniauth初始值设定项中),但仍然使用Desive登录(omniauth的Desive路径)

我留下了让它与设计工作流程一起工作的步骤

Gemfile

gem 'devise'
gem 'omniauth-facebook'
config/initializers/designe.rb

<div><%= link_to image_tag('facebook_login.png'), user_omniauth_authorize_path(:facebook) %></div>
//add this for tell devise the omniauth configuration for facebook
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET']
//your secrets for development, is useful have it this way because you can use different applications for your rails enviroments
ENV['FACEBOOK_APP_ID'] = 'xxxxxxxxxxx';
ENV['FACEBOOK_SECRET'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//override the controller for omniauth callbacks, we will define this later, this is our custom behavior for dealing with what omniauth hash will return to us
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
def self.from_omniauth(auth)
  where(auth.slice(:provider, :uid)).first_or_create do |user|
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
  end
end

def self.new_with_session(params, session)
  if session["devise.user_attributes"]
    new(session["devise.user_attributes"], without_protection: true) do |user|
      user.attributes = params
      user.valid?
    end
  else
    super
  end
end

def password_required?
  super && self.provider.blank?
end

def update_with_password(params, *options)
  if encrypted_password.blank?
    update_attributes(params, *options)
  else
    super
  end
end

def has_no_password?
  self.encrypted_password.blank?
end
config/environments/development.rb

<div><%= link_to image_tag('facebook_login.png'), user_omniauth_authorize_path(:facebook) %></div>
//add this for tell devise the omniauth configuration for facebook
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET']
//your secrets for development, is useful have it this way because you can use different applications for your rails enviroments
ENV['FACEBOOK_APP_ID'] = 'xxxxxxxxxxx';
ENV['FACEBOOK_SECRET'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//override the controller for omniauth callbacks, we will define this later, this is our custom behavior for dealing with what omniauth hash will return to us
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
def self.from_omniauth(auth)
  where(auth.slice(:provider, :uid)).first_or_create do |user|
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
  end
end

def self.new_with_session(params, session)
  if session["devise.user_attributes"]
    new(session["devise.user_attributes"], without_protection: true) do |user|
      user.attributes = params
      user.valid?
    end
  else
    super
  end
end

def password_required?
  super && self.provider.blank?
end

def update_with_password(params, *options)
  if encrypted_password.blank?
    update_attributes(params, *options)
  else
    super
  end
end

def has_no_password?
  self.encrypted_password.blank?
end
routes.rb

<div><%= link_to image_tag('facebook_login.png'), user_omniauth_authorize_path(:facebook) %></div>
//add this for tell devise the omniauth configuration for facebook
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET']
//your secrets for development, is useful have it this way because you can use different applications for your rails enviroments
ENV['FACEBOOK_APP_ID'] = 'xxxxxxxxxxx';
ENV['FACEBOOK_SECRET'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//override the controller for omniauth callbacks, we will define this later, this is our custom behavior for dealing with what omniauth hash will return to us
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
def self.from_omniauth(auth)
  where(auth.slice(:provider, :uid)).first_or_create do |user|
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
  end
end

def self.new_with_session(params, session)
  if session["devise.user_attributes"]
    new(session["devise.user_attributes"], without_protection: true) do |user|
      user.attributes = params
      user.valid?
    end
  else
    super
  end
end

def password_required?
  super && self.provider.blank?
end

def update_with_password(params, *options)
  if encrypted_password.blank?
    update_attributes(params, *options)
  else
    super
  end
end

def has_no_password?
  self.encrypted_password.blank?
end
好的,这是最基本的,现在我们需要自定义回调,定义如何处理facebook提供给我们的信息

我没有一个有效的例子,我的回调实际上非常复杂,因为有很多行为,但我会尝试用记忆来解释

Facebook和所有omniauth策略都将返回一个uid,我们将通过提供商和他的uid(用户id)来识别我们的用户,因此我们将把这个参数添加到我们的用户模型中并进行相应的迁移,这两个都是字符串

rails g migration AddOmniauthToUsers uid provider
rake db:migrate
下一步是配置omniauth回调

controllers/users/omniauth\u callbacks\u controller.rb(请注意,它位于新用户文件夹中)

这里我们有4个新方法,from_omniauth将使用散列中的omniauth参数创建一个新用户(您可以在omniauth facebook gem中看到脸书的omniauth散列),在这里您应该保存另一个值,如image\u link或username,如果您有您的用户模型,我想从这里调用另一个模型用户_配置文件,其中包含所有这些数据,试图将Desive与他自己的东西分开

接下来我们有一个新的with session,它使用我们在回调出错时保存的cookie内容

需要密码?使用密码更新都是我们需要覆盖的设计方法,这一方法抓住了omniauth提供程序和未定义密码情况下的新行为,您可以在表单中使用has\u no\u password?在需要时显示密码字段(不使用密码更新用户属性)


我希望它能帮助你,当你了解工作流程时,定制行为真的很容易,但遗憾的是,大多数指南只允许你复制粘贴代码。

看起来很奇怪。你添加了Omniauth回调控制器吗?感谢所有的帮助。但问题是我有一个脚本干扰了jquery_ujs。一旦我从连接的资产运行良好。