Class: Authorization

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
lib/forcast/models/login/authorization.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.invalidate_token(given_token) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/forcast/models/login/authorization.rb', line 14

def self.invalidate_token(given_token)
  temporal_auth = Authorization.new()

  decoded_token = JsonWebToken.decode(given_token)

  if decoded_token.blank?
    temporal_auth.errors.add(:base, I18n.t("activerecord.errors.token.not_exits"))
  else
    user_id = decoded_token[:user_id]
    created_at_format = decoded_token[:birthday_date]
    created_at = Time.parse(created_at_format)

    auth = Authorization.find_by(user_id:user_id,active_time_stamp:created_at.to_i)
    if auth.present? && auth.operative
      auth.operative = false
      auth.save
    else
      temporal_auth.errors.add(:base, I18n.t("activerecord.errors.token.already_invalid"))
    end
  end

  temporal_auth
end

Instance Method Details

#get_tokenObject



4
5
6
7
8
9
10
11
12
# File 'lib/forcast/models/login/authorization.rb', line 4

def get_token
  jwt_token = if user.present?
                JsonWebToken.encode({user_id:self.user.id, birthday_date:self.created_at})
              elsif admin_user.present?
                JsonWebToken.encode({admin_user_id:self.admin_user.id, birthday_date:self.created_at})
              end

  jwt_token
end