waiting-for-dev / warden-jwt_auth

JWT token authentication with warden
MIT License
113 stars 56 forks source link

Regression STI Subclass Error after file change #29

Closed Ex-Ark closed 3 years ago

Ex-Ark commented 4 years ago

On code reloading, warden-jwt raises an invalid STI Subclass not found error when code is reloaded ActiveRecord::SubclassNotFound (Invalid single-table inheritance type: SuperUser is not a subclass of User)

activerecord (6.0.3.2) lib/active_record/inheritance.rb:241:in `find_sti_class'
activerecord (6.0.3.2) lib/active_record/inheritance.rb:215:in `discriminate_class_for_record'
[....]
devise-jwt (0.8.0) lib/devise/jwt/models/jwt_authenticatable.rb:20:in `find_for_jwt_authentication'
warden-jwt_auth (0.5.0) lib/warden/jwt_auth/payload_user_helper.rb:15:in `find_user'
warden-jwt_auth (0.5.0) lib/warden/jwt_auth/user_decoder.rb:35:in `call'
warden-jwt_auth (0.5.0) lib/warden/jwt_auth/strategy.rb:22:in `authenticate!'

Problem first seen here : https://github.com/waiting-for-dev/devise-jwt/issues/22

Was fixed in warden-jwt_auth here some time ago (0.4.4) : https://github.com/waiting-for-dev/warden-jwt_auth/pull/11

Changes were done in recent update (0.5.0) : https://github.com/waiting-for-dev/warden-jwt_auth/commit/a3e57330a503ecd098d3f3a5b3eeb622eef4bf84

lines that seemed to fix the issue were removed :

config.instance_eval do
      def mappings
        constantize_values(super)
      end

      def revocation_strategies
        constantize_values(super)
      end

      # :reek:UtilityFunction
      def constantize_values(hash)
        hash.each_with_object({}) do |(key, value), memo|
          memo[key] = value.is_a?(String) ? Object.const_get(value) : value
        end
      end
    end
  end

Expected behavior

We shouldn't have to fully reboot the app when developing. It shouldn't raise invalid STI error after code reloading

Actual behavior

ActiveRecord::SubclassNotFound (Invalid single-table inheritance type: SuperUser is not a subclass of User): is raised

Steps to Reproduce the Problem

  1. have STI classes (User, SuperUser)
  2. setup devise jwt + warden jwt (before_action: :authenticate_user!)
  3. boot app in dev env
  4. make change to the code
  5. warden will raises invalid STI

Debugging information

Ex-Ark commented 4 years ago

After some test it is confirmed in my case that regressing from 5.0 to warden-jwt_auth, '0.4.2' fixes this specific issue. So it might indeed come from the change linked above or other changes made between these two versions.

waiting-for-dev commented 4 years ago

Sorry for the late response. I'm quite busy at the moment, but I'll try to find some time for it. Meanwhile, if you come with a fix don't hesitate to give me some feedback or submit a PR. Thanks!

kofiasare commented 4 years ago

After some test it is confirmed in my case that regressing from 5.0 to warden-jwt_auth, '0.4.2' fixes this specific issue. So it might indeed come from the change linked above or other changes made between these two versions.

I can also confirm that after regressing fixes the issue.

rpuzdrowski commented 3 years ago

I've encountered a similar issue in my project. Any change in the code caused an exception: A copy of XXX has been removed from the module tree but is still active - XXX were different ActiveRecord models.

Regressing the version from 5.0 to 0.4.2 resolved the issue.

waiting-for-dev commented 3 years ago

Closing in favor of https://github.com/waiting-for-dev/devise-jwt/pull/186