Skip to content

Commit eca0d34

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 eca0d34

3 files changed

Lines changed: 39 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: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
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
20+
21+
it "generates a webauthn_id on update if missing" do
22+
user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password")
23+
user.update_column(:webauthn_id, nil) # rubocop:disable Rails/SkipsModelValidations
24+
user.reload
25+
26+
user.update!(email: "updated@example.com")
27+
expect(user.webauthn_id).to be_present
28+
end
1529
end
1630

1731
describe "associations" do

spec/devise/models/webauthn_two_factor_authenticatable_spec.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
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
20+
21+
it "generates a webauthn_id on update if missing" do
22+
user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password")
23+
user.update_column(:webauthn_id, nil) # rubocop:disable Rails/SkipsModelValidations
24+
user.reload
25+
26+
user.update!(email: "updated@example.com")
27+
expect(user.webauthn_id).to be_present
28+
end
1529
end
1630

1731
describe "associations" do

0 commit comments

Comments
 (0)