Apartment current_tenant resets to 'public'

2020-04-16 06:20发布

问题:

I use Devise for authentication and Apartment for multi-tenancy support on a SAAS app.

After a failed login, devise "redirects" to the login page (Users::SessionsController#new) and the value of Apartment::Tenant.current which was previously set in a TenantElevator middleware goes back to its default value of "public".

This is happening because Devise isn't actally redirecting to the login page but calling the FailureApp (which renders the login page) with a new rack env and returning its response. The new rack app doesn't have TenantElevator middleware so the tenant isn't set within the rack app.

Does anyone have any idea how to fix this? Maybe a way I can add the TenantElevator middleware to the Failure app?