Skip to content

Commit c723131

Browse files
RenzoMinelliclaude
andauthored
Fix: scope login_with_passkey_form_for to the Devise resource (#134)
* fix: scope form helpers to the Devise resource The form helpers (login_with_passkey_form_for, etc.) used form_with without a scope, so form builder fields like f.check_box :remember_me generated name="remember_me" instead of name="account[remember_me]". Since the passkey strategy reads params[scope][:remember_me], the value was never found. Resolve the scope via Devise::Mapping.find_scope! and pass it to form_with. Switch the internal public_key_credential hidden field to hidden_field_tag so it stays at the top-level params where strategies and controllers expect it. Update controllers to read :name from the now-scoped params. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix rubocop * docs: add changelog entry for scoped form helpers fix Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: narrow scope change to login_with_passkey_form_for only Creation form helpers shouldn't scope fields under the Devise resource since those fields (like :name) are passkey/security key attributes, not account attributes. Only login_with_passkey_form_for needs the scope so that f.check_box :remember_me generates account[remember_me]. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: move changelog entry from Fixed to Added Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fe11674 commit c723131

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Dispatch `webauthn:unsupported` for browsers missing `parseOptionsFromJSON`. [#127](https://github.com/cedarcode/devise-webauthn/pull/127) [@santiagorodriguez96]
8+
- Scope `login_with_passkey_form_for` to the Devise resource so that form builder fields (e.g. `f.check_box :remember_me`) are properly namespaced (e.g. `account[remember_me]`). [#134](https://github.com/cedarcode/devise-webauthn/pull/134) [@RenzoMinelli]
89

910
### Changed
1011

lib/devise/webauthn/helpers/credentials_helper.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ def passkey_creation_form_for(resource_or_resource_name, **options, &block)
1515
end
1616

1717
def login_with_passkey_form_for(resource_or_resource_name, **options, &block)
18+
scope = Devise::Mapping.find_scope!(resource_or_resource_name)
19+
1820
form_with(
19-
**options, url: session_path(resource_or_resource_name), method: :post
21+
**options, scope: scope, url: session_path(resource_or_resource_name), method: :post
2022
) do |f|
2123
tag.webauthn_get(data: { options_url: passkey_authentication_options_path(resource_or_resource_name) }) do
22-
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
24+
concat hidden_field_tag(:public_key_credential, nil, data: { webauthn_target: "response" })
2325
concat capture(f, &block)
2426
end
2527
end

spec/helpers/devise/webauthn/credentials_helper_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def have_hidden_credential_field
101101
page = parse(html)
102102

103103
expect(page).to have_css("input.btn-primary[type='submit']")
104-
expect(page).to have_css("input[type='checkbox'][name='remember_me']")
105-
expect(page).to have_css("label[for='remember_me']", text: "Remember me")
104+
expect(page).to have_css("input[type='checkbox'][name='account[remember_me]']")
105+
expect(page).to have_css("label[for='account_remember_me']", text: "Remember me")
106106
end
107107
end
108108

spec/internal/app/views/devise/sessions/new.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
<%= login_with_passkey_form_for(resource_name, id: "passkey-login") do |form| %>
2727
<div class="field">
28-
<%= check_box_tag "account[remember_me]", id: "passkey_remember_me" %>
29-
<%= label_tag "passkey_remember_me", "Remember me" %>
28+
<%= form.check_box :remember_me %>
29+
<%= form.label :remember_me %>
3030
</div>
3131

3232
<%= form.submit "Log in with passkeys" %>

0 commit comments

Comments
 (0)