Skip to content

Commit 91f1893

Browse files
fix: change webauthn_id generation from after_initialize to before_create
Now that we are backfilling the `webauthn_id` for existing users, we can switch the `after_initialize` for a `before_create`.
1 parent 24b34f1 commit 91f1893

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

lib/devise/models/webauthn_credential_authenticatable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module WebauthnCredentialAuthenticatable
1212

1313
validates :webauthn_id, uniqueness: true, allow_blank: true
1414

15-
after_initialize do
15+
before_validation do
1616
self.webauthn_id ||= WebAuthn.generate_user_id
1717
end
1818
end

spec/devise/models/passkey_authenticatable_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
RSpec.describe Devise::Models::PasskeyAuthenticatable, type: :model do
44
describe "webauthn_id initialization" do
5-
it "generates a webauthn_id on initialize" do
6-
user = Account.new(email: "user@example.com", password: "password", password_confirmation: "password")
5+
it "generates a webauthn_id on create" do
6+
user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password")
77
expect(user.webauthn_id).to be_present
88
end
99

10-
it "keeps existing webauthn_id" do
11-
user = Account.new(email: "user@example.com", password: "password", password_confirmation: "password",
12-
webauthn_id: "custom")
10+
it "does not generate a webauthn_id on initialize" do
11+
user = Account.new(email: "user@example.com", password: "password", password_confirmation: "password")
12+
expect(user.webauthn_id).to be_nil
13+
end
14+
15+
it "keeps webauthn_id if created with one" do
16+
user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password",
17+
webauthn_id: "custom")
1318
expect(user.webauthn_id).to eq("custom")
1419
end
1520
end

spec/devise/models/webauthn_two_factor_authenticatable_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
RSpec.describe Devise::Models::WebauthnTwoFactorAuthenticatable, type: :model do
44
describe "webauthn_id initialization" do
5-
it "generates a webauthn_id on initialize" do
6-
user = Account.new(email: "user@example.com", password: "password", password_confirmation: "password")
5+
it "generates a webauthn_id on create" do
6+
user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password")
77
expect(user.webauthn_id).to be_present
88
end
99

10-
it "keeps existing webauthn_id" do
11-
user = Account.new(email: "user@example.com", password: "password", password_confirmation: "password",
12-
webauthn_id: "custom")
10+
it "does not generate a webauthn_id on initialize" do
11+
user = Account.new(email: "user@example.com", password: "password", password_confirmation: "password")
12+
expect(user.webauthn_id).to be_nil
13+
end
14+
15+
it "keeps webauthn_id if created with one" do
16+
user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password",
17+
webauthn_id: "custom")
1318
expect(user.webauthn_id).to eq("custom")
1419
end
1520
end

0 commit comments

Comments
 (0)