Skip to content

Commit 3804dc8

Browse files
authored
Merge pull request #111 from cedarcode/rm/use-attributes-hash-instead-of-classes
Allow form and button attributes in helper methods
1 parent d7106e9 commit 3804dc8

3 files changed

Lines changed: 57 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@
66

77
- Options for getting or creating passkeys and security keys are now served by dedicated Rails controllers and retrieved via JavaScript fetch requests. [#73](https://github.com/cedarcode/devise-webauthn/pull/73) [@nicolastemciuc]
88
- BREAKING!: Remove helpers for generating WebAuthn options. [#106](https://github.com/cedarcode/devise-webauthn/pull/115) [@nicolastemciuc]
9+
- BREAKING!: Replace `form_classes:` keyword argument with direct keyword arguments in all form helper methods (`passkey_creation_form_for`, `login_with_passkey_form_for`, `security_key_creation_form_for`, `login_with_security_key_form_for`). All options are delegated to `form_with`, allowing you to pass any HTML attributes or form options directly. [@RenzoMinelli]
10+
```erb
11+
<%# Before %>
12+
<%= passkey_creation_form_for(:user, form_classes: "my-class") do |form| %>
13+
...
14+
<% end %>
15+
16+
<%# After %>
17+
<%= passkey_creation_form_for(:user, class: "my-class", id: "my-form", data: { turbo: false }) do |form| %>
18+
...
19+
<% end %>
20+
```
921

10-
- BREAKING: `login_with_passkey_button` and `login_with_security_key_button` helpers have been renamed to `login_with_passkey_form_for` and `login_with_security_key_form_for`. They now take a block and no longer generate the submit button automatically. You need to explicitly add the button inside the block:
22+
- BREAKING!: `login_with_passkey_button` and `login_with_security_key_button` helpers have been renamed to `login_with_passkey_form_for` and `login_with_security_key_form_for`. They now take a block and no longer generate the submit button automatically. You need to explicitly add the button inside the block:
1123
```erb
1224
<%# Before %>
13-
<%%= login_with_passkey_button(:user, "Log in with passkeys") %>
25+
<%= login_with_passkey_button(:user, "Log in with passkeys") %>
1426
1527
<%# After %>
16-
<%%= login_with_passkey_form_for(:user) do |form| %>
17-
<%%= form.submit "Log in with passkeys" %>
18-
<%% end %>
28+
<%= login_with_passkey_form_for(:user) do |form| %>
29+
<%= form.submit "Log in with passkeys" %>
30+
<% end %>
1931
```
2032

2133
### Fixed

lib/devise/webauthn/helpers/credentials_helper.rb

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
module Devise
44
module Webauthn
55
module CredentialsHelper
6-
def passkey_creation_form_for(resource_or_resource_name, form_classes: nil, &block)
6+
def passkey_creation_form_for(resource_or_resource_name, **options, &block)
77
form_with(
8-
url: passkeys_path(resource_or_resource_name),
9-
method: :post,
10-
class: form_classes
8+
**options, url: passkeys_path(resource_or_resource_name), method: :post
119
) do |f|
1210
tag.webauthn_create(data: { options_url: passkey_registration_options_path(resource_or_resource_name) }) do
1311
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
@@ -16,11 +14,9 @@ def passkey_creation_form_for(resource_or_resource_name, form_classes: nil, &blo
1614
end
1715
end
1816

19-
def login_with_passkey_form_for(resource_or_resource_name, form_classes: nil, &block)
17+
def login_with_passkey_form_for(resource_or_resource_name, **options, &block)
2018
form_with(
21-
url: session_path(resource_or_resource_name),
22-
method: :post,
23-
class: form_classes
19+
**options, url: session_path(resource_or_resource_name), method: :post
2420
) do |f|
2521
tag.webauthn_get(data: { options_url: passkey_authentication_options_path(resource_or_resource_name) }) do
2622
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
@@ -29,11 +25,9 @@ def login_with_passkey_form_for(resource_or_resource_name, form_classes: nil, &b
2925
end
3026
end
3127

32-
def security_key_creation_form_for(resource_or_resource_name, form_classes: nil, &block)
28+
def security_key_creation_form_for(resource_or_resource_name, **options, &block)
3329
form_with(
34-
url: second_factor_webauthn_credentials_path(resource_or_resource_name),
35-
method: :post,
36-
class: form_classes
30+
**options, url: second_factor_webauthn_credentials_path(resource_or_resource_name), method: :post
3731
) do |f|
3832
tag.webauthn_create(
3933
data: { options_url: security_key_registration_options_path(resource_or_resource_name) }
@@ -44,11 +38,9 @@ def security_key_creation_form_for(resource_or_resource_name, form_classes: nil,
4438
end
4539
end
4640

47-
def login_with_security_key_form_for(resource_or_resource_name, form_classes: nil, &block)
41+
def login_with_security_key_form_for(resource_or_resource_name, **options, &block)
4842
form_with(
49-
url: two_factor_authentication_path(resource_or_resource_name),
50-
method: :post,
51-
class: form_classes
43+
**options, url: two_factor_authentication_path(resource_or_resource_name), method: :post
5244
) do |f|
5345
tag.webauthn_get(data: {
5446
options_url: security_key_authentication_options_path(resource_or_resource_name)

spec/helpers/devise/webauthn/credentials_helper_spec.rb

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ def have_hidden_credential_field
3838
expect(page).to have_button("Create Passkey")
3939
end
4040

41-
it "accepts form_classes option" do
42-
html = helper.passkey_creation_form_for(user, form_classes: "custom-form") do |form|
41+
it "accepts options passed directly" do
42+
html = helper.passkey_creation_form_for(
43+
user,
44+
class: "custom-form",
45+
id: "passkey-form",
46+
data: { turbo: false }
47+
) do |form|
4348
form.submit "Create"
4449
end
4550

46-
expect(parse(html)).to have_css("form.custom-form")
51+
expect(parse(html)).to have_css('form.custom-form#passkey-form[data-turbo="false"]')
4752
end
4853

4954
it "allows custom content in the block" do
@@ -72,12 +77,17 @@ def have_hidden_credential_field
7277
expect(page).to have_button("Log in with passkeys")
7378
end
7479

75-
it "accepts form_classes option" do
76-
html = helper.login_with_passkey_form_for(:account, form_classes: "passkey-form") do |form|
80+
it "accepts options passed directly" do
81+
html = helper.login_with_passkey_form_for(
82+
:account,
83+
class: "passkey-form",
84+
id: "passkey-login",
85+
data: { controller: "auth" }
86+
) do |form|
7787
form.submit "Login"
7888
end
7989

80-
expect(parse(html)).to have_css("form.passkey-form")
90+
expect(parse(html)).to have_css('form.passkey-form#passkey-login[data-controller="auth"]')
8191
end
8292

8393
it "allows custom content in the block" do
@@ -109,12 +119,17 @@ def have_hidden_credential_field
109119
expect(page).to have_button("Add Security Key")
110120
end
111121

112-
it "accepts form_classes option" do
113-
html = helper.security_key_creation_form_for(user, form_classes: "security-key-form") do |form|
122+
it "accepts options passed directly" do
123+
html = helper.security_key_creation_form_for(
124+
user,
125+
class: "security-key-form",
126+
id: "security-key-registration",
127+
data: { turbo: false }
128+
) do |form|
114129
form.submit "Add"
115130
end
116131

117-
expect(parse(html)).to have_css("form.security-key-form")
132+
expect(parse(html)).to have_css('form.security-key-form#security-key-registration[data-turbo="false"]')
118133
end
119134

120135
it "allows custom content in the block" do
@@ -143,12 +158,17 @@ def have_hidden_credential_field
143158
expect(page).to have_button("Use security key")
144159
end
145160

146-
it "accepts form_classes option" do
147-
html = helper.login_with_security_key_form_for(:account, form_classes: "two-factor-form") do |form|
161+
it "accepts options passed directly" do
162+
html = helper.login_with_security_key_form_for(
163+
:account,
164+
class: "two-factor-form",
165+
id: "security-key-auth",
166+
data: { turbo_method: "post" }
167+
) do |form|
148168
form.submit "Verify"
149169
end
150170

151-
expect(parse(html)).to have_css("form.two-factor-form")
171+
expect(parse(html)).to have_css('form.two-factor-form#security-key-auth[data-turbo-method="post"]')
152172
end
153173

154174
it "allows custom content in the block" do

0 commit comments

Comments
 (0)