Skip to content

Commit

Permalink
Fix :as option in passwordless_for
Browse files Browse the repository at this point in the history
  • Loading branch information
mikker committed Oct 21, 2023
1 parent 6305a59 commit 8c63696
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
11 changes: 10 additions & 1 deletion app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ class Mailer < Passwordless.config.parent_mailer.constantize
# is still in memory (optional)
def sign_in(session, token = nil)
@token = token || session.token
@magic_link = send(:"confirm_#{session.authenticatable_type.tableize}_sign_in_url", session, token)
@magic_link = url_for(
{
controller: "passwordless/sessions",
action: "confirm",
id: session.id,
token: token,
authenticatable: "user",
resource: "users"
}
)
email_field = session.authenticatable.class.passwordless_email_field

mail(
Expand Down
6 changes: 4 additions & 2 deletions lib/passwordless/router_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ module RouterHelpers
# (Default: 'passwordless/sessions')
def passwordless_for(resource, at: :na, as: :na, controller: "passwordless/sessions")
at == :na && at = "/#{resource.to_s}"
as == :na && as = "#{resource.to_s}_"
as == :na && as = resource.to_s

as = as.to_s + "_" unless !as || as.to_s.end_with?("_")

plural = resource.to_s
singular = plural.singularize

defaults = {
authenticatable: singular,
resource: resource,
resource: resource
}

scope(defaults: defaults) do
Expand Down
3 changes: 2 additions & 1 deletion test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

Rails.application.routes.draw do
passwordless_for(:users)
passwordless_for(:admins, controller: 'admin/sessions')
passwordless_for(:admins, controller: "admin/sessions")
passwordless_for(:devs, as: :auth, at: "/")

resources(:users)
resources(:registrations, only: %i[new create])
Expand Down
20 changes: 20 additions & 0 deletions test/passwordless/router_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class RouterHelpersTest < ActionDispatch::IntegrationTest
{method: :get, path: "/users/sign_out"},
defaults
)

assert_equal "/users/sign_in", url_helpers.users_sign_in_path
end

test("map sign in for admin") do
Expand Down Expand Up @@ -100,7 +102,25 @@ class RouterHelpersTest < ActionDispatch::IntegrationTest
{method: :get, path: "/admins/sign_out"},
defaults
)

assert_equal "/admins/sign_in", url_helpers.admins_sign_in_path
end

test(":as option") do
defaults = {authenticatable: "dev", resource: "devs"}
assert_recognizes(
{controller: "passwordless/sessions", action: "new"}.merge(defaults),
{method: :get, path: "/sign_in"},
defaults
)

assert_equal "/sign_in", url_helpers.auth_sign_in_path
end

private

def url_helpers
Rails.application.routes.url_helpers
end
end
end

0 comments on commit 8c63696

Please sign in to comment.