mikker / passwordless

πŸ— Authentication for your Rails app without the icky-ness of passwords
MIT License
1.29k stars 88 forks source link

Add after_session_confirm hook after session confirmation #237

Closed wilburhimself closed 1 month ago

wilburhimself commented 1 month ago

fixes #213

This PR introduces a new after_session_confirm hook to the Passwordless gem. This feature allows users to perform custom actions after a session has been successfully confirmed, such as marking an email as verified or updating user attributes after their first successful login.

Changes

Motivation

Users of the gem often need to perform specific actions after a successful login, such as updating user attributes or triggering other processes. The after_session_confirm hook provides a clean, configurable way to achieve this without modifying the gem's core code.

Implementation Details

How to Use

Users can configure the hook in their Passwordless configuration:

Passwordless.configure do |config|
  config.after_session_confirm = ->(session) {
    user = session.authenticatable
    user.update(email_verified: true)
  }
end

Testing

New tests have been added to SessionsControllerTest to ensure the hook is called correctly in various scenarios:

All existing tests pass with these new changes.

Reviewer Notes

Checklist

mikker commented 1 month ago

Failing test is just rails#main

mikker commented 1 month ago

Great work! β€οΈπŸ§‘πŸ’›πŸ’šπŸ’™πŸ’œ

wkirby commented 4 weeks ago

@mikker when can we expect to see this released?

mikker commented 4 weeks ago

Just pushed 1.8.0 with this change 😊

henrikbjorn commented 4 weeks ago

Am I correct in assuming the controller context is lot in the hook ?

A use case would be for Ahoy to call ahoy.authenticate for analytics. But it seems that can't be done since the hook is "just" a function.

Would it make sense to instead define the hook methods on the controller (kind of like Devise)? Then the Controller context is kept, and it is also easy to set a custom controller to use.

henrikbjorn commented 4 weeks ago

Another note. It is not possible to have two different hooks. E.g one for AdminUsers and one for normal Users. Which having it as a controller method would handle.

mikker commented 4 weeks ago

Good points Henrik!

It could be useful to have the controller context at hand.

Maybe something like if setting the hook setting to a :symbol, a method with that name will be called instead?

Like

class ApplicationController < ...
  private

  def pwless_after_session_confirm(session) # no need to pass request either
    # ...
  end
end

Passwordless.configure do |config|
  config.after_session_confirm = :pwless_after_session_confirm
end

That would make it a non-breaking change.