1- module Authenticatable
2- extend ActiveSupport ::Concern
3-
4- included do
5- before_action :authenticate_request!
6- before_action :set_current_user
7- before_action :set_current_organization
8- end
9-
10- private
11-
12- def authenticate_request!
13- token = extract_token_from_header
14-
15- if token . nil?
16- render_unauthorized ( 'Missing authentication token' )
17- return
18- end
19-
20- begin
21- @jwt_payload = Authentication ::Services ::JwtService . decode ( token )
22- @current_user = User . find ( @jwt_payload [ :user_id ] )
23- @current_organization = @current_user . organization
24-
25- # Update last login time
26- @current_user . update_last_login! if should_update_last_login?
27-
28- rescue Authentication ::Services ::JwtService ::AuthenticationError => e
29- render_unauthorized ( e . message )
30- rescue ActiveRecord ::RecordNotFound
31- render_unauthorized ( 'User not found' )
32- end
33- end
34-
35- def extract_token_from_header
36- auth_header = request . headers [ 'Authorization' ]
37- return nil unless auth_header
38-
39- match = auth_header . match ( /Bearer\s +(.+)/i )
40- match &.[]( 1 )
41- end
42-
43- def current_user
44- @current_user
45- end
46-
47- def current_organization
48- @current_organization
49- end
50-
51- def current_user_id
52- @current_user &.id
53- end
54-
55- def current_organization_id
56- @current_organization &.id
57- end
58-
59- def user_signed_in?
60- @current_user . present?
61- end
62-
63- def require_admin!
64- unless current_user &.admin_or_owner?
65- render_forbidden ( 'Admin access required' )
66- end
67- end
68-
69- def require_owner!
70- unless current_user &.role == 'owner'
71- render_forbidden ( 'Owner access required' )
72- end
73- end
74-
75- def require_role! ( *allowed_roles )
76- unless allowed_roles . include? ( current_user &.role )
77- render_forbidden ( "Required role: #{ allowed_roles . join ( ' or ' ) } " )
78- end
79- end
80-
81- def organization_scoped ( model_class )
82- model_class . where ( organization : current_organization )
83- end
84-
85- def set_current_user
86- # This method can be overridden in controllers if needed
87- end
88-
89- def set_current_organization
90- # This method can be overridden in controllers if needed
91- end
92-
93- def should_update_last_login?
94- return false unless @current_user
95- return true if @current_user . last_login_at . nil?
96-
97- # Only update if last login was more than 1 hour ago to avoid too many updates
98- @current_user . last_login_at < 1 . hour . ago
99- end
100-
101- def render_unauthorized ( message = 'Unauthorized' )
102- render json : {
103- error : {
104- code : 'UNAUTHORIZED' ,
105- message : message
106- }
107- } , status : :unauthorized
108- end
109-
110- def render_forbidden ( message = 'Forbidden' )
111- render json : {
112- error : {
113- code : 'FORBIDDEN' ,
114- message : message
115- }
116- } , status : :forbidden
117- end
1+ module Authenticatable
2+ extend ActiveSupport ::Concern
3+
4+ included do
5+ before_action :authenticate_request!
6+ before_action :set_current_user
7+ before_action :set_current_organization
8+ end
9+
10+ private
11+
12+ def authenticate_request!
13+ token = extract_token_from_header
14+
15+ if token . nil?
16+ render_unauthorized ( 'Missing authentication token' )
17+ return
18+ end
19+
20+ begin
21+ @jwt_payload = Authentication ::Services ::JwtService . decode ( token )
22+ @current_user = User . find ( @jwt_payload [ :user_id ] )
23+ @current_organization = @current_user . organization
24+
25+ # Update last login time
26+ @current_user . update_last_login! if should_update_last_login?
27+
28+ rescue Authentication ::Services ::JwtService ::AuthenticationError => e
29+ render_unauthorized ( e . message )
30+ rescue ActiveRecord ::RecordNotFound
31+ render_unauthorized ( 'User not found' )
32+ end
33+ end
34+
35+ def extract_token_from_header
36+ auth_header = request . headers [ 'Authorization' ]
37+ return nil unless auth_header
38+
39+ match = auth_header . match ( /Bearer\s +(.+)/i )
40+ match &.[]( 1 )
41+ end
42+
43+ def current_user
44+ @current_user
45+ end
46+
47+ def current_organization
48+ @current_organization
49+ end
50+
51+ def current_user_id
52+ @current_user &.id
53+ end
54+
55+ def current_organization_id
56+ @current_organization &.id
57+ end
58+
59+ def user_signed_in?
60+ @current_user . present?
61+ end
62+
63+ def require_admin!
64+ unless current_user &.admin_or_owner?
65+ render_forbidden ( 'Admin access required' )
66+ end
67+ end
68+
69+ def require_owner!
70+ unless current_user &.role == 'owner'
71+ render_forbidden ( 'Owner access required' )
72+ end
73+ end
74+
75+ def require_role! ( *allowed_roles )
76+ unless allowed_roles . include? ( current_user &.role )
77+ render_forbidden ( "Required role: #{ allowed_roles . join ( ' or ' ) } " )
78+ end
79+ end
80+
81+ def organization_scoped ( model_class )
82+ model_class . where ( organization : current_organization )
83+ end
84+
85+ def set_current_user
86+ # This method can be overridden in controllers if needed
87+ end
88+
89+ def set_current_organization
90+ # This method can be overridden in controllers if needed
91+ end
92+
93+ def should_update_last_login?
94+ return false unless @current_user
95+ return true if @current_user . last_login_at . nil?
96+
97+ # Only update if last login was more than 1 hour ago to avoid too many updates
98+ @current_user . last_login_at < 1 . hour . ago
99+ end
100+
101+ def render_unauthorized ( message = 'Unauthorized' )
102+ render json : {
103+ error : {
104+ code : 'UNAUTHORIZED' ,
105+ message : message
106+ }
107+ } , status : :unauthorized
108+ end
109+
110+ def render_forbidden ( message = 'Forbidden' )
111+ render json : {
112+ error : {
113+ code : 'FORBIDDEN' ,
114+ message : message
115+ }
116+ } , status : :forbidden
117+ end
118118end
0 commit comments