0

我刚刚遇到了omniauth-identity,它使用户能够在不使用Facebook、Twitter等的情况下登录和注册。

有一个步骤是你必须创建一个Identity模型(我正在关注这个Railscast):

class Identity < OmniAuth::Identity::Models::ActiveRecord

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                format:     { with: VALID_EMAIL_REGEX },
                uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

现在,我已经有了一个User模型和一个登录和注册系统(按照Ruby on Rails 教程创建):

用户.rb:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation

  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true

  private

  def create_remember_token
   self.remember_token = SecureRandom.urlsafe_base64
  end

所以我有点困惑。是否应该删除与User模型中的身份验证有关的行(例如验证attr_accesiblecreate_remember_token以及用户表中的nameemail字段)?

并删除session_helper.rb呢?

module SessionsHelper
  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end

  def current_user?(user)
    user == current_user
  end

  def signed_in_user
    unless signed_in?
      store_location
      redirect_to signin_url, notice: "Please sign in."
    end
  end

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
  end

  def store_location
    session[:return_to] = request.url
  end
end

因为,如果我错了,请纠正我,但我认为omniauth-identity也可以处理(除了current_user部分。

4

1 回答 1

0

创建身份模型主要用于向多个提供者进行身份验证。下面是如何去做的一个很好的描述:

https://github.com/intridea/omniauth/wiki/Managing-Multiple-Providers

它回答了您的问题,用户模型应该是什么,身份模型应该是什么。

于 2013-02-21T16:55:02.510 回答