2

このRailscastに従ってomniauth-identityを実装しました。

だから私はこのようなものになりました:

ID.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

同じ問題に遭遇したので、少しテストしました。私の場合 (Mongoid) では、FactoryGirl は実際にパスワードを暗号化します。

FactoryGirl.define do
  factory :local_identity do

    #snip

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

次のように使用できます (3 行目の 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 に答える