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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 5

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

      field :unconfirmed_email,    type: String
      field :confirmation_token,   type: String
      field :confirmation_sent_at, type: Time
      field :confirmed_at,         type: Time
    end

    validate :validate_confirmation, if: :confirmed_at_changed?

    after_create do
      unless confirmed_at || confirmation_sent_at || self['invitation_token']
        send_confirmation_instructions
      end
    end
  end
end

Instance Method Details

#confirmObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 46

def confirm
  self.confirmed_at = Time.current
  self.confirmation_token = nil

  if unconfirmed_email
    email_field = RailsJwtAuth.email_field_name

    self[email_field] = unconfirmed_email
    self.unconfirmed_email = nil

    # supports email confirmation attr_accessor validation
    if respond_to?("#{email_field}_confirmation")
      instance_variable_set("@#{email_field}_confirmation", self[email_field])
    end
  end

  save
end

#confirmed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 42

def confirmed?
  confirmed_at.present?
end

#send_confirmation_instructionsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 28

def send_confirmation_instructions
  if confirmed? && !unconfirmed_email
    errors.add(RailsJwtAuth.email_field_name, :already_confirmed)
    return false
  end

  self.confirmation_token = generate_confirmation_token
  self.confirmation_sent_at = Time.current
  return false unless save

  RailsJwtAuth.send_email(:confirmation_instructions, self)
  true
end

#skip_confirmationObject



65
66
67
68
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 65

def skip_confirmation
  self.confirmed_at = Time.current
  self.confirmation_token = nil
end

#update_email(params) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/concerns/rails_jwt_auth/confirmable.rb', line 70

def update_email(params)
  email_field = RailsJwtAuth.email_field_name.to_sym
  params = HashWithIndifferentAccess.new(params)

  # email change must be protected by password
  password_error = if (password = params[:password]).blank?
                     :blank
                   elsif !authenticate(password)
                     :invalid
                   end

  self.email = params[email_field]
  self.confirmation_token = generate_confirmation_token
  self.confirmation_sent_at = Time.current

  valid? # validates first other fields
  errors.add(:password, password_error) if password_error
  errors.add(email_field, :not_change) unless email_changed?

  return false unless errors.empty?

  # move email to unconfirmed_email field and restore
  self.unconfirmed_email = email
  self.email = email_was

  return false unless save

  deliver_email_changed_emails

  true
end