Sha256: bbcf985d583763a73218d46182c8795d38bb249799e48847918241cfaa674091

Contents?: true

Size: 691 Bytes

Versions: 7

Compression:

Stored size: 691 Bytes

Contents

module Commons
  module Authentication
    module AuthenticateByJWT
      def authorize_jwt!
        raise Commons::Errors::Unauthorized if jwt.blank?

        begin
          decoded = JSONWebToken.decode(jwt)
          @current_user = UserRepository.instance.find_by(id: decoded[:user_id])
        rescue ActiveRecord::RecordNotFound => e
          raise Commons::Errors::Unauthorized
        rescue JWT::DecodeError => e
          raise Commons::Errors::Unauthorized
        end
      end

      private

      def jwt
        jwt ||= request.headers['Authorization']
        return nil unless jwt.instance_of? String

        jwt = jwt.split(' ').last if jwt
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
commons_yellowme-0.16.0 lib/commons/authentication/authenticate_by_jwt.rb
commons_yellowme-0.15.0 lib/commons/authentication/authenticate_by_jwt.rb
commons_yellowme-0.12.0 lib/commons/authentication/authenticate_by_jwt.rb
commons_yellowme-0.11.3 lib/commons/authentication/authenticate_by_jwt.rb
commons_yellowme-0.11.2 lib/commons/authentication/authenticate_by_jwt.rb
commons_yellowme-0.11.1 lib/commons/authentication/authenticate_by_jwt.rb
commons_yellowme-0.11.0 lib/commons/authentication/authenticate_by_jwt.rb