Skip to content

Commit

Permalink
Add spec and docs for after_magic_link_sent_path_for
Browse files Browse the repository at this point in the history
  • Loading branch information
abevoelker committed Sep 8, 2023
1 parent 6011767 commit 835a5d1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* `MagicLinksController` now uses gem source instead of needing to be generated from a template
* `magic_link_(path|url)` view helpers are now implemented for all resources (cleans up mailer view template)
* Tokenizer encoding now supports `:expires_at` option (#19, #21 - thanks @JoeyLeadJig and @bvsatyaram!)
* Users will be redirected after magic link is sent (customized using `after_magic_link_sent_path_for`)

### Bugfixes

Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,45 @@ To customize the magic link email body, edit `app/views/devise/mailer/magic_link

To customise email headers (including the email subject as well as more unusual headers like `X-Entity-Ref-ID`) pass them in a hash to `resource.send_magic_link` in `SessionsController`, eg. `resource.send_magic_link(create_params[:remember_me], subject: "Your login link has arrived!")`.

### Redirecting after magic link is sent

After a magic link is sent, the user will be redirected. By default, the location is
chosen based on these values, in order:

1. `session["#{resource/scope}_return_to"]` session key (e.g. `session["user_return_to"]`)
2. `:#{resource/scope}_root` route (e.g. `:user_root`)
3. Otherwise, use the global `:root` route

To customize the redirect, you can write a custom `after_magic_link_sent_path_for` helper,
similar to [how Devise's `after_sign_in_path_for` helper works][after_sign_in_path_for]:

```ruby
class ApplicationController < ActionController::Base
def after_magic_link_sent_path_for(resource_or_scope)
"/foo"
end
end
```

[after_sign_in_path_for]: https://github.com/heartcombo/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update

If you need more complex behavior, you can always write a custom sessions controller:

```ruby
# app/controllers/custom_sessions_controller.rb
class CustomSessionsController < Devise::Passwordless::SessionsController
def create
# your custom logic
end
end
# config/routes.rb
Rails.application.routes.draw do
devise_for :users,
controllers: { sessions: "custom_sessions" }
end
```

## Tokenizers

The algorithm used to encode and decode tokens can be fully customized and swapped
Expand Down
8 changes: 7 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ to have a successful upgrade:
and setting the `config.responder` value in your Devise configuration
(see Devise Turbo upgrade guide: https://github.com/heartcombo/devise/wiki/How-To:-Upgrade-to-Devise-4.9.0-%5BHotwire-Turbo-integration%5D)
* Resource `#send_magic_link` now uses keyword arguments instead of positional arguments.
* The `#send_magic_link` method now uses keyword arguments instead of positional arguments.
* Change any instances of
```ruby
Expand All @@ -75,3 +75,9 @@ to have a successful upgrade:
```ruby
user.send_magic_link(remember_me: true, subject: "Custom email subject")
```
* After sending a magic link, users will now be redirected rather than
re-rendering the sign-in form.
* [See the README][after-magic-link-sent-readme] for details on how to customize the redirect behavior
[after-magic-link-sent-readme]: https://github.com/abevoelker/devise-passwordless#redirecting-after-magic-link-is-sent
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AfterMagicLinkSentSessionsController < Devise::Passwordless::SessionsController
def after_magic_link_sent_path_for(*args)
test_custom_after_magic_link_sent_redirect_baz_path
end
end
8 changes: 8 additions & 0 deletions spec/dummy_app_config/shared_source/all/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
get "foo", to: ->(env) { [200, {}, ["foo"]] }
get "bar", to: ->(env) { [200, {}, ["bar"]] }
end
# Set up a namespace for testing custom after_magic_link_sent_path_for
namespace "test_custom_after_magic_link_sent_redirect" do
devise_for :passwordless_users,
controllers: {
sessions: "after_magic_link_sent_sessions"
}
get "baz", to: ->(env) { [200, {}, ["baz"]] }
end

root to: "welcome#index"
end
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,19 @@
expect(page.body).to eq("bar")
end
end

context "redirect sessions controller" do
let(:sign_in_path) { "/test_custom_after_magic_link_sent_redirect/passwordless_users/sign_in" }
let!(:user) { PasswordlessUser.create(email: email) }

it "uses the after_magic_link_sent_path_for" do
visit sign_in_path

fill_in "Email", with: email
click_button "Log in"

expect(current_path).to eq("/test_custom_after_magic_link_sent_redirect/baz")
expect(page.body).to eq("baz")
end
end
end

0 comments on commit 835a5d1

Please sign in to comment.