Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/v1/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create
def portal_url
customer = current_organization.customers.find_by(external_id: params[:customer_external_id])

result = ::CustomerPortal::GenerateUrlService.call(customer:)
result = ::CustomerPortal::GenerateUrlService.call(customer:, return_to: params[:return_to])

if result.success?
render(
Expand Down
14 changes: 11 additions & 3 deletions app/services/customer_portal/generate_url_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

module CustomerPortal
class GenerateUrlService < BaseService
def initialize(customer:)
def initialize(customer:, return_to: nil)
@customer = customer
@return_to = return_to

super
end
Expand All @@ -14,13 +15,20 @@ def call
public_authenticator = ActiveSupport::MessageVerifier.new(ENV["SECRET_KEY_BASE"])
message = public_authenticator.generate(customer.id, expires_in: 12.hours)

result.url = "#{ENV["LAGO_FRONT_URL"]}/customer-portal/#{message}"
url = "#{ENV["LAGO_FRONT_URL"]}/customer-portal/#{message}"
# return_to lets the host app self-heal a stale portal tab: when the
# JWT expires, lago-front bounces back to this URL with ?refresh=1 so
# the host app's billing page can mint a fresh URL.
if return_to.present?
url += "?return_to=#{CGI.escape(return_to)}"
end
result.url = url

result
end

private

attr_reader :customer
attr_reader :customer, :return_to
end
end
Loading