Sha256: 8598c20259bde3c871c3fb04724e34873b591a4cd154412d811a797e07689b67

Contents?: true

Size: 1.29 KB

Versions: 15

Compression:

Stored size: 1.29 KB

Contents

require 'jwt'

module ForestLiana
  class ApplicationController < ActionController::Base
    before_filter :authenticate_user_from_jwt

    def current_user
      @jwt_decoded_token
    end

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

    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

    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

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
forest_liana-1.3.24 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.23 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.22 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.21 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.20 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.19 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.18 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.17 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.16 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.15 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.14 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.13 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.12 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.11 app/controllers/forest_liana/application_controller.rb
forest_liana-1.3.10 app/controllers/forest_liana/application_controller.rb