Omniauth Facebook 身份验证 +身份使用相同的模型而不是两个

发布于 2025-01-07 19:10:41 字数 2320 浏览 3 评论 0 原文

我已经根据本教程设置了 Omniauth Facebook 身份验证: http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/ 现在我尝试使用相同的用户模型而不是像本教程中那样使用单独的身份模型将其与omniauth-identity结合起来: http://railscasts.com/episodes/304-omniauth-identity?view=asciicast ,但我无法获取它才能正常工作。

这是我的initializers/omniauth.rb 文件:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'xxxxx', 'xxxxx'
  provider :identity, :fields => [:email], :model => User
end

我已将omniauth-identity 所需的“password_digest”列添加到我的用户模型/表中,并将用户模型代码

class User < ActiveRecord::Base
  has_many :authorizations
  #validates :name, :email, :presence => true

  def add_provider(auth_hash)
    # check if the provider already exists, so we don't add it twice
    unless authorizations.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
      Authorization.create :user => self, :provider => auth_hash["provider"], :uid => auth_hash["uid"], :token => auth_hash["token"]
    end
  end
end

更改为,

class User < OmniAuth::Identity::Models::ActiveRecord
  ...
end

但是当我这样做时,授权中的代码创建用户和授权模型的模型无法正常工作 当用户模型从 ActiveRecord::Base 扩展时,记录创建得很好,但是当我从 OmniAuth::Identity::Models::ActiveRecord 扩展用户模型时,创建新授权时,用户模型不会存储在数据库中。

这是授权模型代码:

class Authorization < ActiveRecord::Base
  belongs_to :user
  validates :provider, :uid, :presence => true

  def self.find_or_create(auth_hash)
    unless auth = find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
      user = User.create :name => auth_hash["info"]["name"], :email => auth_hash["info"]["email"]
      auth = create :user => user, :provider => auth_hash["provider"], :uid => auth_hash["uid"], :token => auth_hash["credentials"]["token"]
    end

    auth
  end
end

当我从 ActiveRecord::Base 扩展用户模型并尝试使用 Identity 创建新的注册时,我收到此错误:

ActiveRecord::UnknownAttributeError
unknown attribute: password

有什么方法可以使其以这种方式工作吗?我现在不知道该怎么办。

I've setup Omniauth Facebook authentication according to this tutorial: http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/
And now I'm trying to combine it with omniauth-identity using the same User model instead of a separate Identity model as in this tutorial: http://railscasts.com/episodes/304-omniauth-identity?view=asciicast , but I cannot get it to work properly.

This is is my initializers/omniauth.rb file:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'xxxxx', 'xxxxx'
  provider :identity, :fields => [:email], :model => User
end

I've added 'password_digest' column that is needed by omniauth-identity to my User model/table and changed the User model code

from

class User < ActiveRecord::Base
  has_many :authorizations
  #validates :name, :email, :presence => true

  def add_provider(auth_hash)
    # check if the provider already exists, so we don't add it twice
    unless authorizations.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
      Authorization.create :user => self, :provider => auth_hash["provider"], :uid => auth_hash["uid"], :token => auth_hash["token"]
    end
  end
end

to

class User < OmniAuth::Identity::Models::ActiveRecord
  ...
end

but when I do that the code in the Authorization model that creates the User and the Authorization models does not work properly
When the User model extends from ActiveRecord::Base the records are created just fine but when I extend the user model from OmniAuth::Identity::Models::ActiveRecord the user model is not stored in the database when you create a new authorization.

This is the Authorization model code:

class Authorization < ActiveRecord::Base
  belongs_to :user
  validates :provider, :uid, :presence => true

  def self.find_or_create(auth_hash)
    unless auth = find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
      user = User.create :name => auth_hash["info"]["name"], :email => auth_hash["info"]["email"]
      auth = create :user => user, :provider => auth_hash["provider"], :uid => auth_hash["uid"], :token => auth_hash["credentials"]["token"]
    end

    auth
  end
end

When I extend the User model from ActiveRecord::Base and try to create a new registration with Identity I get this error:

ActiveRecord::UnknownAttributeError
unknown attribute: password

Is there any way to get this working this way? I don't know what to do now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2025-01-14 19:10:41

不确定你是否仍然遇到这个问题,但也许 interwebz 上的某个人会遇到这个问题。

我刚刚通过博客发布了一个解决方案,应该可以解决您的问题:

http://bernardi.me/2012/09/using-multiple-omniauth-providers-with-omniauth-identity-on-the-main-user-model/

not sure you're still having the problem, but maybe someone on the interwebz will.

I just posted a solution on by blog, should solve your problems:

http://bernardi.me/2012/09/using-multiple-omniauth-providers-with-omniauth-identity-on-the-main-user-model/

南风几经秋 2025-01-14 19:10:41

尝试添加 attr_accessor :password 并且可能是 attr_accessor :email

try to add attr_accessor :password and may be attr_accessor :email

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文