【问题标题】:Omniauth not workingOmniauth 不工作
【发布时间】:2013-02-28 16:51:34
【问题描述】:

所以我已经安装了 Omniauth gem,它目前是用 Devise 实现的。

Devise 运行良好,但是设置 Omniauth 是一场战斗,我仍在努力解决这个问题。我正在使用 Omniauth 和 Twitter 身份验证。

问题:当我点击“使用 twitter 登录”图标时,它会将我重新定向到 twitter,然后提示我输入我的twitter 凭据..一切都很好,直到它开始重定向(回调)。

当它尝试重定向到我的应用时,我收到以下错误:

AuthenticationsController#create 中的 NoMethodError

undefined method `[]' for nil:NilClass

app/models/user.rb:14:in `apply_omniauth'
app/controllers/authentications_controller.rb:14:in `create'

注册控制器:

class RegistrationsController < Devise::RegistrationsController
  def create
    super
    session[:omniauth] = nil unless @user.new_record?
  end

  private

  def build_resource(*args)
    super
    if session[:omniauth]
      @user.apply_omniauth(session[:omniauth])
      @user.valid?
    end
  end
end

身份验证控制器:

class AuthenticationsController < ApplicationController
  def create
    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
end

User.rb(用户控制器)

class User < ActiveRecord::Base
  has_many :authentications

  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable, :timeoutable and :activatable
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation
  # attr_accessible :title, :body

  def apply_omniauth(omniauth)
    self.email = omniauth['user_info']['email'] if email.blank?
    authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])

  end

  def password_required?
    (authentications.empty? || !password.blank?) && super
  end
end

请阅读更新:

我刚刚遇到了以下 RailsCast。

教程说要运行:

rails g nifty:scaffold authentication user_id:integer \ 提供者:字符串 uid:字符串索引创建销毁

但我的机器上没有漂亮的脚手架,我只是跑了

rails g 脚手架身份验证 user_id:integer \ 提供者:字符串 uid:字符串索引创建销毁

其行为不同。而不是创建存根'index','create', 和“销毁”控制器方法,它在数据库中创建了字段。

如何删除字段?

【问题讨论】:

  • 您希望删除哪些字段?

标签: ruby-on-rails ruby-on-rails-3 gem omniauth


【解决方案1】:

尝试更换

omniauth['user_info']['email']

omniauth['email']

你可以使用这样的包装方法

更新:omniauth 的包装器

def omni_conversion(omniauth)
    {
      # Required For Social Network Creation
      access_token: omniauth.credentials.token,
      link: omniauth.extra.raw_info.link,
      provider: omniauth.provider,
      providerid: omniauth.uid,

      # Required For User Creation
      birthday: omniauth.extra.raw_info.birthday,
      email: omniauth.info.email,
      first_name: omniauth.info.first_name,
      gender: omniauth.extra.raw_info.gender,
      last_name: omniauth.info.last_name,
      middle_name: omniauth.info.middle_name,
      picture: omniauth.info.image,
      timezone: omniauth.info.timezone,
      username: omniauth.extra.raw_info.username
     }
  end

omniauth = omni_conversion(omniauth)

优点是您可以直接使用符号。您可以将它们直接传递给模型。

【讨论】:

  • 感谢@Abibullah Rahamathulah,但我现在收到此错误:“无法批量分配受保护的属性:provider, uid”
  • 当你看到这种错误时,你应该按照@swap.nil给出的建议。
【解决方案2】:

在你的User.rb 第 14 行

应该是omniauth['info']['email']而不是omniauth['user_info']['email']

nifty generators 你应该有

gem "nifty-generators", group: :development

在你的 Gemfile 中

尝试rails g scaffold --help 以查看脚手架生成器的可用选项

你也可以看看这个惊人的 rails-cast generators-in-rails-3

【讨论】:

  • 感谢@swap.nil,但现在我收到此错误:“无法批量分配受保护的属性:provider, uid”我该怎么办?
  • :provider:uid 添加到您的身份验证模型白名单属性,即添加到 attr_accessible :provider, :uid api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/…
  • @TonyP 尝试更频繁地使用api.rubyonrails.org,这是一个学习Rails 的好地方;-)
  • 伟大的来源,适当地指出。我已将 attr_accessible :provider, :id 添加到身份验证模型中,但我得到: AuthenticationsController:Class 的未定义方法 `attr_accessible'
  • @TonyP 白名单仅适用于ActiveRecord::Base,不适用于ActionController::Base,无需在Controller 中包含attr_accessible
猜你喜欢
  • 2013-09-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2013-07-30
  • 2018-08-27
  • 1970-01-01
相关资源
最近更新 更多