Sha256: abd3ec296adae23ed83e25747d8fd932054f98ddc64ead2488c3944c4b26030c

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

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

  included do
    validates :email, presence: true, if: lambda { uid_and_provider_defined? && email_provider? }
    validates :email, :devise_token_auth_email => true, allow_nil: true, allow_blank: true, if: lambda { uid_and_provider_defined? && email_provider? }
    validates_presence_of :uid, if: lambda { uid_and_provider_defined? && !email_provider? }

    # only validate unique emails among email registration users
    validates :email, uniqueness: { case_sensitive: false, scope: :provider }, on: :create, if: lambda { uid_and_provider_defined? && email_provider? }

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

  protected

  def uid_and_provider_defined?
    defined?(provider) && defined?(uid)
  end

  def email_provider?
    provider == 'email'
  end

  def sync_uid
    unless self.new_record?
      return if devise_modules.include?(:confirmable) && !@bypass_confirmation_postpone && postpone_email_change?
    end
    self.uid = email if uid_and_provider_defined? && email_provider?
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
devise_token_auth-1.2.5 app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb
devise_token_auth-1.2.4 app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb
devise_token_auth-1.2.2 app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb
devise_token_auth-1.2.1 app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb