Module: RailsJwtAuth::Authenticatable
- Defined in:
- app/models/concerns/rails_jwt_auth/authenticatable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #destroy_auth_token(token) ⇒ Object
- #regenerate_auth_token(token = nil) ⇒ Object
- #to_token_payload(_request = nil) ⇒ Object
- #update_with_password(params) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'app/models/concerns/rails_jwt_auth/authenticatable.rb', line 5 def self.included(base) base.extend(ClassMethods) base.class_eval do if defined?(Mongoid) && ancestors.include?(Mongoid::Document) field :password_digest, type: String field :auth_tokens, type: Array if RailsJwtAuth.simultaneous_sessions > 0 elsif defined?(ActiveRecord) && ancestors.include?(ActiveRecord::Base) serialize :auth_tokens, Array end has_secure_password end end |
Instance Method Details
#destroy_auth_token(token) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'app/models/concerns/rails_jwt_auth/authenticatable.rb', line 33 def destroy_auth_token(token) if RailsJwtAuth.simultaneous_sessions > 1 tokens = auth_tokens || [] update_attribute(:auth_tokens, tokens - [token]) else update_attribute(:auth_tokens, []) end end |
#regenerate_auth_token(token = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/concerns/rails_jwt_auth/authenticatable.rb', line 20 def regenerate_auth_token(token = nil) new_token = SecureRandom.base58(24) if RailsJwtAuth.simultaneous_sessions > 1 tokens = ((auth_tokens || []) - [token]).last(RailsJwtAuth.simultaneous_sessions - 1) update_attribute(:auth_tokens, (tokens + [new_token]).uniq) else update_attribute(:auth_tokens, [new_token]) end new_token end |
#to_token_payload(_request = nil) ⇒ Object
60 61 62 63 64 65 66 |
# File 'app/models/concerns/rails_jwt_auth/authenticatable.rb', line 60 def to_token_payload(_request=nil) if RailsJwtAuth.simultaneous_sessions > 0 {auth_token: regenerate_auth_token} else {id: id.to_s} end end |
#update_with_password(params) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/models/concerns/rails_jwt_auth/authenticatable.rb', line 42 def update_with_password(params) current_password_error = if (current_password = params.delete(:current_password)).blank? 'blank' elsif !authenticate(current_password) 'invalid' end # abort reset password if exists to allow save self.reset_password_token = self.reset_password_sent_at = nil if reset_password_token assign_attributes(params) valid? # validates first other fields errors.add(:current_password, current_password_error) if current_password_error errors.add(:password, 'blank') if params[:password].blank? errors.empty? ? save : false end |