@@ -10,6 +10,8 @@ class ApplicationController < ActionController::Base # rubocop:todo Metrics/Clas
1010 before_action :check_platform_setup
1111 before_action :set_locale
1212 before_action :store_user_location! , if : :storable_location?
13+
14+ before_action :set_platform_invitation
1315 before_action :check_platform_privacy
1416 # The callback which stores the current location must be added before you authenticate the user
1517 # as `authenticate_user!` (or whatever your resource is) will halt the filter chain and redirect
@@ -24,7 +26,7 @@ class ApplicationController < ActionController::Base # rubocop:todo Metrics/Clas
2426 rescue_from Pundit ::NotAuthorizedError , with : :user_not_authorized
2527 rescue_from StandardError , with : :handle_error
2628
27- helper_method :default_url_options
29+ helper_method :default_url_options , :valid_platform_invitation_token_present?
2830
2931 def self . default_url_options
3032 super . merge ( locale : I18n . locale )
@@ -56,15 +58,58 @@ def check_platform_setup
5658 redirect_to setup_wizard_path
5759 end
5860
61+ def set_platform_invitation
62+ # Only proceed if there's an invitation token in the URL or already in the session.
63+ return unless params [ :invitation_code ] . present? || session [ :platform_invitation_token ] . present?
64+
65+ # Check if the session token has expired.
66+ if session [ :platform_invitation_expires_at ] . present? && Time . current > session [ :platform_invitation_expires_at ]
67+ session . delete ( :platform_invitation_token )
68+ session . delete ( :platform_invitation_expires_at )
69+ return
70+ end
71+
72+ if params [ :invitation_code ] . present?
73+ # On first visit with the invitation code, update the session with the token and a new expiry.
74+ token = params [ :invitation_code ]
75+ session [ :platform_invitation_token ] = token
76+ session [ :platform_invitation_expires_at ] ||= Time . current + platform_invitation_expiry_time
77+ else
78+ # If no params, simply use the token stored in the session.
79+ token = session [ :platform_invitation_token ]
80+ end
81+
82+ return unless token . present?
83+
84+ @platform_invitation = ::BetterTogether ::PlatformInvitation . pending . find_by ( token : token )
85+
86+ unless @platform_invitation
87+ session . delete ( :platform_invitation_token )
88+ session . delete ( :platform_invitation_expires_at )
89+ end
90+ end
91+
5992 def check_platform_privacy
6093 return if helpers . host_platform . privacy_public?
6194 return if current_user
6295 return unless BetterTogether . user_class . any?
96+ return if valid_platform_invitation_token_present?
6397
6498 flash [ :error ] = I18n . t ( 'globals.platform_not_public' )
6599 redirect_to new_user_session_path ( locale : I18n . locale )
66100 end
67101
102+ def valid_platform_invitation_token_present?
103+ token = session [ :platform_invitation_token ]
104+ return false unless token . present?
105+
106+ return false if session [ :platform_invitation_expires_at ] . present? && Time . current > session [ :platform_invitation_expires_at ]
107+
108+ ::BetterTogether ::PlatformInvitation . pending . exists? ( token : token )
109+ end
110+
111+ private
112+
68113 def handle404
69114 render_404
70115 end
@@ -171,8 +216,18 @@ def after_sign_in_path_for(resource)
171216 end
172217 end
173218
219+ def after_inactive_sign_up_path_for ( resource )
220+ new_user_session_path if helpers . host_platform &.private?
221+ super
222+ end
223+
174224 def after_sign_out_path_for ( _resource_or_scope )
175225 BetterTogether . base_path_with_locale
176226 end
227+
228+ # Configurable expiration time (e.g., 30 minutes)
229+ def platform_invitation_expiry_time
230+ 30 . minutes
231+ end
177232 end
178233end
0 commit comments