Sha256: 4fa84422196e0a53b8abd47594608d6753f8658c7cb7d038a616fa12e77abf03

Contents?: true

Size: 792 Bytes

Versions: 1

Compression:

Stored size: 792 Bytes

Contents

module DeviseTokenAuth::Concerns::UserOmniauthCallbacks
  extend ActiveSupport::Concern

  included do
    validates :email, presence: true, email: true, if: Proc.new { |u| u.provider == 'email' }
    validates_presence_of :uid, if: Proc.new { |u| u.provider != 'email' }

    # only validate unique emails among email registration users
    validate :unique_email_user, on: :create

    # keep uid in sync with email
    before_save :sync_uid
    before_create :sync_uid
  end

  protected

  # only validate unique email among users that registered by email
  def unique_email_user
    if provider == 'email' and self.class.where(provider: 'email', email: email).count > 0
      errors.add(:email, :taken)
    end
  end

  def sync_uid
    self.uid = email if provider == 'email'
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
devise_token_auth-0.1.40 app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb