Skip to content

Commit 468c637

Browse files
authored
Merge pull request #114 from cedarcode/rm/stop-requiring-resource
Use resource_name instead of resource object
2 parents 6749e1b + 31ba580 commit 468c637

7 files changed

Lines changed: 29 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
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]
99

10+
### Fixed
11+
12+
- Fix form helpers (`passkey_creation_form_for`, `login_with_passkey_button`, `security_key_creation_form_for`, `login_with_security_key_button`) to accept a `resource_name` instead of requiring the `resource` object from the view context. [#114](https://github.com/cedarcode/devise-webauthn/pull/114) [@RenzoMinelli]
13+
1014
## [v0.3.0](https://github.com/cedarcode/devise-webauthn/compare/v0.2.2...v0.3.0/) - 2026-01-16
1115

1216
### Added

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,18 @@ $ bin/rails generate devise:webauthn:views -v passkeys
145145
```
146146

147147
### Helper methods
148-
Devise::Webauthn provides helpers that can be used in your views. For example, for a resource named `user`, you can use the following helpers:
148+
Devise::Webauthn provides helpers that can be used in your views. These helpers accept either a resource name (e.g., `:user`) or a resource object (e.g., `@user`) as the first argument.
149+
150+
For example, for a resource named `user`, you can use the following helpers:
149151

150152
To add a button for logging in with passkeys:
151153
```erb
152-
<%= login_with_passkey_button("Log in with passkeys", session_path: user_session_path) %>
154+
<%= login_with_passkey_button_for(:user, "Log in with passkeys") %>
153155
```
154156

155157
To add a passkeys creation form:
156158
```erb
157-
<%= passkey_creation_form_for(current_user) do |form| %>
159+
<%= passkey_creation_form_for(:user) do |form| %>
158160
<%= form.label :name, 'Passkey name' %>
159161
<%= form.text_field :name, required: true %>
160162
<%= form.submit 'Create Passkey' %>

app/views/devise/passkeys/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= passkey_creation_form_for(resource) do |form| %>
1+
<%= passkey_creation_form_for(resource_name) do |form| %>
22
<%= form.label :name, 'Passkey name' %>
33
<%= form.text_field :name, required: true %>
44
<%= form.submit 'Create Passkey' %>

app/views/devise/second_factor_webauthn_credentials/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= security_key_creation_form_for(resource) do |form| %>
1+
<%= security_key_creation_form_for(resource_name) do |form| %>
22
<%= form.label :name, 'Security Key name' %>
33
<%= form.text_field :name, required: true %>
44
<%= form.submit 'Create Security Key' %>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
</div>
2424
<% end %>
2525

26-
<%= login_with_passkey_button("Log in with passkeys", session_path: session_path(resource_name)) %>
26+
<%= login_with_passkey_button_for(resource_name, "Log in with passkeys") %>
2727

2828
<%= render "devise/shared/links" %>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<%= login_with_security_key_button('Use security key', resource: @resource) %>
1+
<%= login_with_security_key_button_for(resource_name, 'Use security key') %>

lib/devise/webauthn/helpers/credentials_helper.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,59 @@
33
module Devise
44
module Webauthn
55
module CredentialsHelper
6-
def passkey_creation_form_for(resource, form_classes: nil, &block)
6+
def passkey_creation_form_for(resource_or_resource_name, form_classes: nil, &block)
77
form_with(
8-
url: passkeys_path(resource),
8+
url: passkeys_path(resource_or_resource_name),
99
method: :post,
1010
class: form_classes
1111
) do |f|
12-
tag.webauthn_create(data: { options_url: passkey_registration_options_path(resource) }) do
12+
tag.webauthn_create(data: { options_url: passkey_registration_options_path(resource_or_resource_name) }) do
1313
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
1414
concat capture(f, &block)
1515
end
1616
end
1717
end
1818

19-
def login_with_passkey_button(text = nil, session_path:, button_classes: nil, form_classes: nil, &block)
19+
def login_with_passkey_button_for(resource_or_resource_name, text = nil, button_classes: nil,
20+
form_classes: nil, &block)
2021
form_with(
21-
url: session_path,
22+
url: session_path(resource_or_resource_name),
2223
method: :post,
2324
class: form_classes
2425
) do |f|
25-
tag.webauthn_get(data: { options_url: passkey_authentication_options_path(resource) }) do
26+
tag.webauthn_get(data: { options_url: passkey_authentication_options_path(resource_or_resource_name) }) do
2627
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
2728

2829
concat f.button(text, type: "submit", class: button_classes, &block)
2930
end
3031
end
3132
end
3233

33-
def security_key_creation_form_for(resource, form_classes: nil, &block)
34+
def security_key_creation_form_for(resource_or_resource_name, form_classes: nil, &block)
3435
form_with(
35-
url: second_factor_webauthn_credentials_path(resource),
36+
url: second_factor_webauthn_credentials_path(resource_or_resource_name),
3637
method: :post,
3738
class: form_classes
3839
) do |f|
3940
tag.webauthn_create(
40-
data: { options_url: security_key_registration_options_path(resource) }
41+
data: { options_url: security_key_registration_options_path(resource_or_resource_name) }
4142
) do
4243
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
4344
concat capture(f, &block)
4445
end
4546
end
4647
end
4748

48-
def login_with_security_key_button(text = nil, resource:, button_classes: nil, form_classes: nil, &block)
49+
def login_with_security_key_button_for(resource_or_resource_name, text = nil, button_classes: nil,
50+
form_classes: nil, &block)
4951
form_with(
50-
url: two_factor_authentication_path(resource),
52+
url: two_factor_authentication_path(resource_or_resource_name),
5153
method: :post,
5254
class: form_classes
5355
) do |f|
54-
tag.webauthn_get(data: { options_url: security_key_authentication_options_path(resource) }) do
56+
tag.webauthn_get(data: {
57+
options_url: security_key_authentication_options_path(resource_or_resource_name)
58+
}) do
5559
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
5660
concat f.button(text, type: "submit", class: button_classes, &block)
5761
end

0 commit comments

Comments
 (0)