Sha256: f84205a808d9197e6288354a797bb4079fb94dc2badb768f2712f36124d67a5b

Contents?: true

Size: 808 Bytes

Versions: 1

Compression:

Stored size: 808 Bytes

Contents

module Daidan
  module Middleware
    class JwtAuthentication
      def initialize(app)
        @app = app
      end

      def call(env)
        auth_header = env['HTTP_AUTHORIZATION']

        if auth_header && auth_header.start_with?('Bearer ')
          token = auth_header.split(' ').last
          begin
            payload, = JWT.decode(
              token,
              ENV['JWT_SECRET'],
              true,
              algorithm: 'HS256'
            )

            env['current_user_id'] = payload['user_id']
          rescue JWT::ExpiredSignature
            env['current_user_id'] = nil
          rescue JWT::DecodeError
            env['current_user_id'] = nil
          end
        else
          env['current_user_id'] = nil
        end

        @app.call(env)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
daidan-0.2.0 lib/daidan/middleware/jwt_authentication.rb