Class: ForestLiana::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/forest_liana/application_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_user_from_jwtObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/forest_liana/application_controller.rb', line 35

def authenticate_user_from_jwt
  if request.headers['Authorization']
    begin
      token = request.headers['Authorization'].split.second
      @jwt_decoded_token = JWT.decode(token, ForestLiana.auth_key, true, {
        algorithm: 'HS256',
        leeway: 30
      }).try(:first)
    rescue JWT::ExpiredSignature, JWT::VerificationError
      render json: { error: 'expired_token' }, status: 401, serializer: nil
    end
  else
    render nothing: true, status: 401
  end
end

#forest_userObject



9
10
11
# File 'app/controllers/forest_liana/application_controller.rb', line 9

def forest_user
  @jwt_decoded_token
end

#serialize_model(model, options = {}) ⇒ Object



13
14
15
16
# File 'app/controllers/forest_liana/application_controller.rb', line 13

def serialize_model(model, options = {})
  options[:is_collection] = false
  JSONAPI::Serializer.serialize(model, options)
end

#serialize_models(models, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/forest_liana/application_controller.rb', line 18

def serialize_models(models, options = {})
  options[:is_collection] = true
  json = JSONAPI::Serializer.serialize(models, options)

  if options[:count]
    json[:meta] = {} unless json[:meta]
    json[:meta][:count] = options[:count]
  end

  if !options[:has_more].nil?
    json[:meta] = {} unless json[:meta]
    json[:meta][:has_more] = options[:has_more]
  end

  json
end