Module: RailsJwtAuth::Confirmable

Defined in:
app/models/concerns/rails_jwt_auth/confirmable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 41

def self.included(base)
  if base.ancestors.include? Mongoid::Document
    # include GlobalID::Identification to use deliver_later method
    # http://edgeguides.rubyonrails.org/active_job_basics.html#globalid
    base.send(:include, GlobalID::Identification) if RailsJwtAuth.deliver_later

    base.send(:field, :email,                type: String)
    base.send(:field, :unconfirmed_email,    type: String)
    base.send(:field, :confirmation_token,   type: String)
    base.send(:field, :confirmation_sent_at, type: Time)
    base.send(:field, :confirmed_at,         type: Time)
  end

  base.send(:validate, :validate_confirmation, if: :confirmed_at_changed?)

  base.send(:after_create) do
    send_confirmation_instructions unless confirmed_at || confirmation_sent_at
  end

  base.send(:before_update) do
    if email_changed? && email_was && !confirmed_at_changed?
      self.unconfirmed_email = email
      self.email = email_was
      send_confirmation_instructions
    end
  end
end

Instance Method Details

#confirm!Object



21
22
23
24
25
26
27
28
29
30
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 21

def confirm!
  self.confirmed_at = Time.now.utc

  if unconfirmed_email
    self.email = unconfirmed_email
    self.unconfirmed_email = nil
  end

  save
end

#confirmation_in_progress?Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 36

def confirmation_in_progress?
  !confirmed_at && confirmation_token && confirmation_sent_at &&
    (Time.now < (confirmation_sent_at + RailsJwtAuth.confirmation_expiration_time))
end

#confirmed?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 17

def confirmed?
  confirmed_at.present?
end

#send_confirmation_instructionsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 3

def send_confirmation_instructions
  if confirmed? && !unconfirmed_email_changed?
    errors.add(:email, I18n.t('rails_jwt_auth.errors.already_confirmed'))
    return false
  end

  self.confirmation_token = SecureRandom.base58(24)
  self.confirmation_sent_at = Time.now
  return false unless save

  mailer = Mailer.confirmation_instructions(self)
  RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
end

#skip_confirmation!Object



32
33
34
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 32

def skip_confirmation!
  self.confirmed_at = Time.now.utc
end