Skip to content

Commit 573fe0b

Browse files
committed
Fix: Add local: true and custom controller for invitation acceptance
Fixes the "Invitation token can't be blank" error that occurs when new users accept email invitations and try to set their password. Root cause: PR #6528 refactored form_for to form_with, but form_with defaults to remote: true (AJAX submission) which can cause issues with hidden field submission, particularly the invitation_token field. Changes made: 1. Added local: true to form_with to use standard form submission 2. Removed readonly: true from invitation_token hidden field (unnecessary and potentially problematic with form_with) 3. Created custom Users::InvitationsController to: - Explicitly ensure invitation_token is set on resource in edit action - Explicitly permit invitation_token in strong parameters - Add logging to help debug token issues 4. Updated routes to use custom invitations controller The custom controller provides better control over parameter handling and includes debugging logs to identify any future token issues. Related to PR #6528 (form helper refactor)
1 parent c4a8e3a commit 573fe0b

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
class Users::InvitationsController < Devise::InvitationsController
4+
# GET /users/invitation/accept?invitation_token=abcdef123456
5+
def edit
6+
set_minimum_password_length
7+
# Ensure the invitation_token is set on the resource from the URL parameter
8+
resource.invitation_token = params[:invitation_token]
9+
10+
Rails.logger.info "Invitation Edit: Token from params: #{params[:invitation_token]}"
11+
Rails.logger.info "Invitation Edit: Token set on resource: #{resource.invitation_token}"
12+
13+
render :edit
14+
end
15+
16+
# PUT /users/invitation
17+
def update
18+
Rails.logger.info "Invitation Update: Params received: #{update_resource_params.inspect}"
19+
Rails.logger.info "Invitation Update: invitation_token in params: #{update_resource_params[:invitation_token]}"
20+
21+
super
22+
end
23+
24+
protected
25+
26+
# Permit the invitation_token parameter
27+
def update_resource_params
28+
params.require(resource_name).permit(:invitation_token, :password, :password_confirmation)
29+
end
30+
end

app/views/devise/invitations/edit.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<div class="password-box px-4 pb-3">
22
<h2 class="my-3">Send invitation</h2>
33

4-
<%= form_with(model: resource, as: resource_name, url: invitation_path(resource_name), html: {method: :put}) do |f| %>
4+
<%= form_with(model: resource, as: resource_name, url: invitation_path(resource_name), local: true, html: {method: :put}) do |f| %>
55
<%= render "/shared/error_messages", resource: resource %>
6-
<%= f.hidden_field :invitation_token, readonly: true %>
6+
<%= f.hidden_field :invitation_token %>
77

88
<% if f.object.class.require_password_on_accepting %>
99
<div class="input-style-1">

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
mount Rswag::Api::Engine => "/api-docs"
66

77
devise_for :all_casa_admins, path: "all_casa_admins", controllers: {sessions: "all_casa_admins/sessions"}
8-
devise_for :users, controllers: {sessions: "users/sessions", passwords: "users/passwords"}
8+
devise_for :users, controllers: {sessions: "users/sessions", passwords: "users/passwords", invitations: "users/invitations"}
99
authenticate :all_casa_admins do
1010
mount PgHero::Engine, at: "pg_dashboard", constraints: lambda { |request|
1111
admin = request.env["warden"].user(:all_casa_admin)

0 commit comments

Comments
 (0)