waiting-for-dev / devise-jwt

JWT token authentication with devise and rails
MIT License
1.26k stars 130 forks source link

Feature Request: Dynamic Method for Setting JWT Expiration Time #264

Closed aamra12 closed 3 months ago

aamra12 commented 1 year ago

Hi there,

I'm working on a multi-tenant application where each tenant has its own JWT expiration time. Currently, devise-jwt lacks a dynamic method to set the JWT expiration time based on runtime values. I'm looking to set the expiration time based on tenant-specific values.

Here's a snippet of what I'd like to achieve:

config.jwt do |jwt|
  jwt.secret = ....
  jwt.request_formats = ....
  jwt.dispatch_requests = ....
  jwt.expiration_time = TENANT_BASED_TIMEOUT  # Dynamically set based on the current tenant.
end

Is there a planned feature for this in an upcoming release? Alternatively, do you have a recommended workaround for this scenario? Thanks for your help!

waiting-for-dev commented 1 year ago

Given that's set in the configuration, it looks a bit tricky to implement. What would you need access to?

aamra12 commented 1 year ago

I'm currently working on a feature where the JWT expiration_time is determined by a value stored in our database, allowing for dynamic token expiration periods based on specific criteria or user settings. However, I've encountered a challenge when trying to set this dynamic value within the ruby devise.rb initializer:

 ...
 jwt.expiration_time = MyDatabaseModel.retrieve_expiration_time
 ...
 end 

The method MyDatabaseModel.retrieve_expiration_time is intended to pull the expiration_time from the database. Given the nature of initializers, it seems I'm running into challenges with this dynamic assignment.

I was wondering if there's an established pattern or recommendation for achieving this with devise-jwt? If not, please suggest a approach that you consider would be best for this approach. Thank you for your time and for the great work on this gem!

waiting-for-dev commented 1 year ago

Not sure, but maybe you can set it in a Rails.application.config.to_prepare block?