@@ -14,70 +14,61 @@ module Authenticatable
1414
1515 private
1616
17- def authenticate_request! # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
17+ def authenticate_request!
1818 token = extract_token_from_header
1919
2020 if token . nil?
2121 render_unauthorized ( 'Missing authentication token' )
2222 return
2323 end
2424
25- begin
26- @jwt_payload = JwtService . decode ( token )
27-
28- # Reject refresh tokens used as access tokens.
29- # Access tokens for users carry type: 'access'.
30- # Access tokens for players carry entity_type: 'player' AND type: 'access'.
31- # Refresh tokens carry type: 'refresh' and must never authenticate a request.
32- unless valid_access_token_type? ( @jwt_payload )
33- raise JwtService ::TokenInvalidError , 'Invalid token type'
34- end
35-
36- if @jwt_payload [ :entity_type ] == 'player'
37- # ── Player token ──────────────────────────────────────────────────────
38- # Free agents (auto-cadastro via ArenaBR) têm organization_id: nil
39- @current_player = Player . unscoped . find ( @jwt_payload [ :player_id ] )
40-
41- org_id = @jwt_payload [ :organization_id ]
42- @current_organization = org_id . present? ? Organization . find ( org_id ) : nil
43-
44- Current . organization_id = @current_organization &.id
45- org_label = @current_organization &.id || 'free_agent'
46- Rails . logger . info ( "[AUTH] Player token: player_id=#{ @current_player . id } org=#{ org_label } " )
47- return
48- end
49-
50- # ── Regular user token ────────────────────────────────────────────────
51- # Bypass RLS for authentication queries - we need to find the user before we can set RLS context
52- @current_user = User . unscoped . find ( @jwt_payload [ :user_id ] )
53- @current_organization = @current_user . organization
54-
55- # Set request-scoped attributes for OrganizationScoped models (thread-safe)
56- Current . organization_id = @current_organization . id
57- Current . user_id = @current_user . id
58- Current . user_role = @current_user . role
59-
60- # Debug log in production to verify Current is being set
61- Rails . logger . info ( "[AUTH] Set Current.organization_id=#{ Current . organization_id } for user #{ @current_user . email } " )
62-
63- # Update last login time (uses update_column which skips callbacks/audit logs)
64- @current_user . update_last_login! if should_update_last_login?
65- rescue JwtService ::AuthenticationError => e
66- Rails . logger . error ( "JWT Authentication error: #{ e . class } - #{ e . message } " )
67- render_unauthorized ( e . message )
68- rescue ActiveRecord ::RecordNotFound => e
69- Rails . logger . error ( "User not found during authentication: #{ e . message } " )
70- render_unauthorized ( 'User not found' )
71- rescue StandardError => e
72- Rails . logger . error ( "Unexpected authentication error: #{ e . class } - #{ e . message } " )
73- Rails . logger . error ( e . backtrace . join ( "\n " ) )
74- render json : {
75- error : {
76- code : 'INTERNAL_ERROR' ,
77- message : 'An internal error occurred'
78- }
79- } , status : :internal_server_error
25+ perform_authentication ( token )
26+ end
27+
28+ def perform_authentication ( token )
29+ @jwt_payload = JwtService . decode ( token )
30+
31+ # Reject refresh tokens used as access tokens.
32+ # Refresh tokens carry type: 'refresh' and must never authenticate a request.
33+ raise JwtService ::TokenInvalidError , 'Invalid token type' unless valid_access_token_type? ( @jwt_payload )
34+
35+ if @jwt_payload [ :entity_type ] == 'player'
36+ authenticate_player_token
37+ else
38+ authenticate_user_token
8039 end
40+ rescue JwtService ::AuthenticationError => e
41+ Rails . logger . error ( "JWT Authentication error: #{ e . class } - #{ e . message } " )
42+ render_unauthorized ( e . message )
43+ rescue ActiveRecord ::RecordNotFound => e
44+ Rails . logger . error ( "User not found during authentication: #{ e . message } " )
45+ render_unauthorized ( 'User not found' )
46+ rescue StandardError => e
47+ Rails . logger . error ( "Unexpected authentication error: #{ e . class } - #{ e . message } " )
48+ Rails . logger . error ( e . backtrace . join ( "\n " ) )
49+ render json : { error : { code : 'INTERNAL_ERROR' , message : 'An internal error occurred' } } ,
50+ status : :internal_server_error
51+ end
52+
53+ def authenticate_player_token
54+ # Free agents (auto-cadastro via ArenaBR) têm organization_id: nil
55+ @current_player = Player . unscoped . find ( @jwt_payload [ :player_id ] )
56+ org_id = @jwt_payload [ :organization_id ]
57+ @current_organization = org_id . present? ? Organization . find ( org_id ) : nil
58+ Current . organization_id = @current_organization &.id
59+ org_label = @current_organization &.id || 'free_agent'
60+ Rails . logger . info ( "[AUTH] Player token: player_id=#{ @current_player . id } org=#{ org_label } " )
61+ end
62+
63+ def authenticate_user_token
64+ # Bypass RLS for authentication queries - we need to find the user before we can set RLS context
65+ @current_user = User . unscoped . find ( @jwt_payload [ :user_id ] )
66+ @current_organization = @current_user . organization
67+ Current . organization_id = @current_organization . id
68+ Current . user_id = @current_user . id
69+ Current . user_role = @current_user . role
70+ Rails . logger . info ( "[AUTH] Set Current.organization_id=#{ Current . organization_id } for user #{ @current_user . email } " )
71+ @current_user . update_last_login! if should_update_last_login?
8172 end
8273
8374 def extract_token_from_header
0 commit comments