Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/generators/authkit/templates/app/models/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.user_from_confirmation_token(token) ⇒ Object



72
73
74
# File 'lib/generators/authkit/templates/app/models/user.rb', line 72

def self.user_from_confirmation_token(token)
  user_from_token(token)
end

.user_from_remember_token(token) ⇒ Object

These methods are a little redundant, but give you the opportunity to insert expiry for any of these token based authentication strategies. For example:

def self.user_from_remember_token(token)
  user = user_from_token(token)
  user = nil if user && user.remember_token_created_at < 30.days.ago
  user
end


64
65
66
# File 'lib/generators/authkit/templates/app/models/user.rb', line 64

def self.user_from_remember_token(token)
  user_from_token(token)
end

.user_from_reset_password_token(token) ⇒ Object



68
69
70
# File 'lib/generators/authkit/templates/app/models/user.rb', line 68

def self.user_from_reset_password_token(token)
  user_from_token(token)
end

.user_from_token(token) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/generators/authkit/templates/app/models/user.rb', line 34

def self.user_from_token(token)
  verifier = ActiveSupport::MessageVerifier.new(Rails.application.config.secret_key_base)
  id = verifier.verify(token)
  User.where(id: id).first
rescue ActiveSupport::MessageVerifier::InvalidSignature
  nil
end

.user_from_unlock_token(token) ⇒ Object



76
77
78
# File 'lib/generators/authkit/templates/app/models/user.rb', line 76

def self.user_from_unlock_token(token)
  user_from_token(token)
end

Instance Method Details

#change_password(password, password_confirmation) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/generators/authkit/templates/app/models/user.rb', line 133

def change_password(password, password_confirmation)
  self.password = password
  self.password_confirmation = password_confirmation

  # Don't nil out the token unless the changes are valid as it may be
  # needed again (when re-rendering the form, for instance)
  if valid?
    self.reset_password_token = nil
    self.reset_password_token_created_at = nil
  end

  self.save
end

#clear_remember_tokenObject



98
99
100
101
102
# File 'lib/generators/authkit/templates/app/models/user.rb', line 98

def clear_remember_token
  self.remember_token = nil
  self.remember_token_created_at = nil
  self.save
end

#display_nameObject



80
81
82
# File 'lib/generators/authkit/templates/app/models/user.rb', line 80

def display_name
  [first_name, last_name].compact.join(" ")
end

#email_confirmedObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/generators/authkit/templates/app/models/user.rb', line 118

def email_confirmed
  return false if self.confirmation_token.blank? || self.confirmation_email.blank?

  self.email = self.confirmation_email

  # Don't nil out the token unless the changes are valid as it may be
  # needed again (when re-rendering the form, for instance)
  if valid?
    self.confirmation_token = nil
    self.confirmation_token_created_at = nil
  end

  self.save
end

#send_confirmationObject



111
112
113
114
115
116
# File 'lib/generators/authkit/templates/app/models/user.rb', line 111

def send_confirmation
  return false unless set_token(:confirmation_token)

  # TODO: insert your mailer logic here
  true
end

#send_reset_passwordObject



104
105
106
107
108
109
# File 'lib/generators/authkit/templates/app/models/user.rb', line 104

def send_reset_password
  return false unless set_token(:reset_password_token)

  # TODO: insert your mailer logic here
  true
end

#send_welcomeObject



93
94
95
96
# File 'lib/generators/authkit/templates/app/models/user.rb', line 93

def send_welcome
  # TODO: insert your mailer logic here
  true
end

#set_token(field) ⇒ Object

The tokens created by this method have unique indexes but they are digests of the id which is unique. Because of this we shouldn’t see a conflict. If we do, however we want the ActiveRecord::StatementInvalid or ActiveRecord::RecordNotUnique exeception to bubble up.



46
47
48
49
50
51
52
# File 'lib/generators/authkit/templates/app/models/user.rb', line 46

def set_token(field)
  return unless self.persisted?
  verifier = ActiveSupport::MessageVerifier.new(Rails.application.config.secret_key_base)
  self.send("#{field}_created_at=", Time.now)
  self.send("#{field}=", verifier.generate(self.id))
  self.save
end

#track_sign_in(ip) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/generators/authkit/templates/app/models/user.rb', line 84

def (ip)
  self. += 1
  self. = self.
  self. = self.
  self. = Time.now
  self. = ip
  self.save
end