2

我在这个Railscast之后实现了omniauth-identity

所以我最终得到了这样的结果:

身份.rb:

class Identity < OmniAuth::Identity::Models::ActiveRecord
  attr_accessible :name, :email, :password, :password_confirmation

  validates_presence_of :name
  validates_uniqueness_of :email
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
end

用户.rb:

class User < ActiveRecord::Base

  def self.from_omniauth(auth)
    find_by_provider_and_uid(auth["provider"], auth["uid"]) || create_with_omniauth(auth)
  end

  def self.create_with_omniauth(auth)
    create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.email = auth["info"]["email"]
    end
  end

架构.rb:

  create_table "identities", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "password_digest"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end

  create_table "users", :force => true do |t|
    t.datetime "created_at",                    :null => false
    t.datetime "updated_at",                    :null => false
    t.boolean  "admin",      :default => false
    t.string   "uid"
    t.string   "provider"
    t.string   "name"
    t.string   "email"
  end

现在,我不太确定如何使用FactoryGirl创建用户,因为密码正在由Identity模型处理。

如果我省略密码,我可以创建用户,但如果没有密码,我将无法登录。

我真的不知道如何使这件事发挥作用。有一个通用的解决方案吗?

4

1 回答 1

1

我遇到了同样的问题,所以我做了一点测试。FactoryGirl 实际上确实在我的案例(Mongoid)中加密了密码,因此它只是存储明文密码以供以后检索:

FactoryGirl.define do
  factory :local_identity do

    #snip

    sequence :password do |n|
      "MyPassword#{n}"
    end
  end
end

可以这样使用(注意第三行的password_digest):

i = FactoryGirl.build(:local_identity)
i.password # MyPassword1
Bcrypt::Password.new(i.password_digest) == i.password # true
于 2013-03-29T09:13:15.903 回答