Skip to content

Commit

Permalink
Merge pull request #26 from Vagab/passkey-keyword-param
Browse files Browse the repository at this point in the history
Add passkey keyword to `after_passkey_authentication`
  • Loading branch information
tcannonfodder authored Jun 24, 2023
2 parents 990e042 + bbcc840 commit eaaaf65
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- https://github.com/ruby-passkeys/devise-passkeys/pull/11
- Document `Devise::Passkeys::Model`
- https://github.com/ruby-passkeys/devise-passkeys/pull/12
- Add `passkey:` keyword param to `after_passkey_authentication` callback
- https://github.com/ruby-passkeys/devise-passkeys/pull/26

## [0.1.0] - 2023-05-07

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class User < ApplicationRecord
self.find_by(id: passkey.user.id)
end

def after_passkey_authentication
def after_passkey_authentication(passkey:)
end
end
```
Expand Down
6 changes: 4 additions & 2 deletions lib/devise/passkeys/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ module Models
module PasskeyAuthenticatable
# This is a callback that is called right after a successful passkey authentication.
#
# By default, it is a no-op, but you can override it in your model for any custom behavior (such as notifying users of a new login).
def after_passkey_authentication; end
# By default, it is a no-op, but you can override it in your model for any custom behavior
# (such as notifying the user of a new login).
# @param passkey [String] the passkey that was used for authentication
def after_passkey_authentication(passkey:); end
end
end
end
2 changes: 1 addition & 1 deletion lib/devise/passkeys/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def authenticate!

if validate(resource)
remember_me(resource)
resource.after_passkey_authentication
resource.after_passkey_authentication(passkey: passkey)
record_passkey_use(passkey: passkey)
success!(resource)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def root_path

response_json = JSON.parse(response.body)

assert_equal User.after_passkey_authentication_passkey, passkey.label
assert_equal ({ "reauthentication_token" => session["user_current_reauthentication_token"] }), response_json
assert_nil session["user_current_reauthentication_challenge"]
end
Expand Down
7 changes: 7 additions & 0 deletions test/rails_app/app/active_record/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ class User < ActiveRecord::Base
has_many :passkeys, class_name: "UserPasskey", dependent: :destroy

validates :sign_in_count, presence: true

cattr_accessor :after_passkey_authentication_passkey

def after_passkey_authentication(passkey:)
# used to check in our test if the callbacks were called
@@after_passkey_authentication_passkey = passkey.label
end
end
7 changes: 7 additions & 0 deletions test/rails_app/app/mongoid/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ class User
field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
field :unlock_token, type: String # Only if unlock strategy is :email or :both
field :locked_at, type: Time

cattr_accessor :after_passkey_authentication_passkey

def after_passkey_authentication(passkey:)
# used to check in our test if the callbacks were called
@@after_passkey_authentication_passkey = passkey.label
end
end

0 comments on commit eaaaf65

Please sign in to comment.