Skip to content

Commit 26fc469

Browse files
committed
fix: simplify thread local variable management
1 parent 38b1eb2 commit 26fc469

1 file changed

Lines changed: 6 additions & 17 deletions

File tree

app/controllers/concerns/authenticatable.rb

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module Authenticatable
1010
before_action :authenticate_request!
1111
before_action :set_current_user
1212
before_action :set_current_organization
13-
around_action :set_organization_context
1413
end
1514

1615
private
@@ -30,6 +29,12 @@ def authenticate_request!
3029
@current_user = User.unscoped.find(@jwt_payload[:user_id])
3130
@current_organization = @current_user.organization
3231

32+
# Set thread-local variables for OrganizationScoped models
33+
# This is needed early for update_last_login! and will be maintained by set_organization_context
34+
Thread.current[:current_organization_id] = @current_organization.id
35+
Thread.current[:current_user_id] = @current_user.id
36+
Thread.current[:current_user_role] = @current_user.role
37+
3338
# Update last login time (uses update_column which skips callbacks/audit logs)
3439
@current_user.update_last_login! if should_update_last_login?
3540
rescue Authentication::Services::JwtService::AuthenticationError => e
@@ -133,20 +138,4 @@ def render_forbidden(message = 'Forbidden')
133138
}
134139
}, status: :forbidden
135140
end
136-
137-
def set_organization_context
138-
# Set thread-local variables for OrganizationScoped concern
139-
if current_organization && current_user
140-
Thread.current[:current_organization_id] = current_organization.id
141-
Thread.current[:current_user_id] = current_user.id
142-
Thread.current[:current_user_role] = current_user.role
143-
end
144-
145-
yield
146-
ensure
147-
# Always reset thread-local variables
148-
Thread.current[:current_organization_id] = nil
149-
Thread.current[:current_user_id] = nil
150-
Thread.current[:current_user_role] = nil
151-
end
152141
end

0 commit comments

Comments
 (0)