Module: ActsAsHocUser::ClassMethods

Defined in:
lib/acts_as_hoc_user/acts_as_hoc_user.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_hoc_user(_options = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/acts_as_hoc_user/acts_as_hoc_user.rb', line 34

def acts_as_hoc_user(_options = {})
  has_secure_password
  validates_presence_of :email
  validates :email, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
  validates :password, length: { minimum: ActsAsHocUser.configuration.min_password_length }, if: -> { password.present? }
end

#authenticate_with_authentication_token(token) ⇒ Object



20
21
22
23
24
25
# File 'lib/acts_as_hoc_user/acts_as_hoc_user.rb', line 20

def authenticate_with_authentication_token(token)
  decoded_auth_token = JsonWebToken.decode(token)
  return nil if decoded_auth_token.nil?
  user = User.find(decoded_auth_token[:user_id])
  return user
end

#authenticate_with_credentials(email, password, expiration = 14.days.from_now) ⇒ Object



13
14
15
16
17
18
# File 'lib/acts_as_hoc_user/acts_as_hoc_user.rb', line 13

def authenticate_with_credentials(email, password, expiration = 14.days.from_now)
  user = User.find_by(email:email)
  return nil if user.nil?
  return user.authentication_token(expiration) if user.authenticate(password)
  nil
end

#authenticate_with_http_headers(headers = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/acts_as_hoc_user/acts_as_hoc_user.rb', line 27

def authenticate_with_http_headers(headers = {})
  if headers['Authorization'].present?
    return authenticate_with_authentication_token(headers['Authorization'].split(' ').last)
  end
  return nil
end