Skip to content

Commit

Permalink
Save controller names for url helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mikker committed Nov 9, 2023
1 parent a7891a8 commit 648ce48
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
9 changes: 8 additions & 1 deletion app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ def create
Passwordless.config.after_session_save.call(@session)
end

resource = @session.authenticatable.model_name.to_s.tableize.to_sym
controller = Passwordless.controllers[resource]

redirect_to(
url_for(id: @session.identifier, action: "show"),
url_for(
id: @session.identifier,
action: "show",
controller:
),
flash: {notice: I18n.t("passwordless.sessions.create.email_sent")}
)
else
Expand Down
9 changes: 6 additions & 3 deletions app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ class Mailer < Passwordless.config.parent_mailer.constantize
# is still in memory (optional)
def sign_in(session, token = nil)
@token = token || session.token

resource = session.authenticatable.model_name.to_s.tableize.to_sym

@magic_link = url_for(
{
controller: "passwordless/sessions",
controller: Passwordless.controllers[resource],
action: "confirm",
id: session.identifier,
token: token,
authenticatable: "user",
resource: "users"
authenticatable: session.authenticatable.model_name.singular,
resource:
}
)
email_field = session.authenticatable.class.passwordless_email_field
Expand Down
4 changes: 4 additions & 0 deletions lib/passwordless/router_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module Passwordless
cattr_accessor(:controllers) { {} }

# Helpers for generating passwordless routes.
module RouterHelpers
# Generates passwordless routes for a given Model
Expand Down Expand Up @@ -35,6 +37,8 @@ def passwordless_for(resource, at: :na, as: :na, controller: "passwordless/sessi
resource: resource
}

Passwordless.controllers[resource] = controller

scope(defaults: defaults) do
get("#{at}/sign_in", to: "#{controller}#new", as: :"#{as}sign_in")
post("#{at}/sign_in", to: "#{controller}#create")
Expand Down

0 comments on commit 648ce48

Please sign in to comment.