Fix remote signer QR sizing and expiry UI

This commit is contained in:
raucao committed 2026-11-27 16:07:32 +04:00
1 parent b8256ec388
commit fffd13b814
8 files changed
+93 -20

No files matched your search

@@ -23,22 +23,26 @@
<div data-controller="nostr-signer-connect"
data-nostr-signer-connect-status-url-value="<%= @status_url %>">
<% unless @pending_session.bunker? %>
<p class="mt-3 text-sm">Scan this QR code with your signer app, or copy the connection URI:</p>
<div data-nostr-signer-connect-target="details">
<p class="mt-3 text-sm">Scan this QR code with your signer app, or copy the connection URI:</p>
<div class="mt-4 flex justify-center rounded-lg bg-white p-3">
<%= qr_svg %>
<div class="mt-4 flex justify-center">
<div class="w-56 max-w-full rounded-lg bg-white p-3">
<%= qr_svg %>
</div>
</div>
<p class="mt-3">
<input type="text" readonly value="<%= @pending_session.nostrconnect_uri %>"
class="w-full text-xs" data-nostr-signer-connect-target="uri" />
</p>
<p class="mt-2">
<button type="button" class="btn-sm btn-gray"
data-action="nostr-signer-connect#copy">
Copy connection URI
</button>
</p>
</div>
<p class="mt-3">
<input type="text" readonly value="<%= @pending_session.nostrconnect_uri %>"
class="w-full text-xs" data-nostr-signer-connect-target="uri" />
</p>
<p class="mt-2">
<button type="button" class="btn-sm btn-gray"
data-action="nostr-signer-connect#copy">
Copy connection URI
</button>
</p>
<% end %>
<p class="mt-4 text-sm text-gray-600" data-nostr-signer-connect-target="status">
@@ -55,6 +59,10 @@
<p class="mt-2 hidden text-sm text-red-700" data-nostr-signer-connect-target="error"></p>
<p class="mt-4 hidden" data-nostr-signer-connect-target="retry">
<%= button_to "Try again", @create_url, method: :post, class: "btn-sm btn-gray" %>
</p>
<details class="mt-4 text-sm">
<summary class="cursor-pointer text-gray-600">Prefer a bunker:// URI?</summary>
<%= form_with url: @create_url, method: :post, local: true, class: "mt-3" do %>
@@ -18,10 +18,11 @@ class NostrSignerConnectComponent < ViewComponent::Base
RQRCode::QRCode.new(@pending_session.nostrconnect_uri).as_svg(
color: "000",
shape_rendering: "crispEdges",
module_size: 5,
module_size: 4,
viewbox: true,
standalone: true,
use_path: true,
svg_attributes: { class: "inline-block" }
svg_attributes: { class: "block h-auto w-full" }
).html_safe
end
@@ -59,6 +59,11 @@ module Settings
signer_session.update_column(:last_status_poll_at, Time.current)
if signer_session.status == "pending" && signer_session.expires_at < Time.current
signer_session.expire!
return render json: { status: "expired", error: signer_session.error }
end
if signer_session.verified? || signer_session.connected?
connect_account!(signer_session)
elsif signer_session.disconnecting?
@@ -71,6 +71,11 @@ module Users
signer_session.update_column(:last_status_poll_at, Time.current)
if signer_session.status == "pending" && signer_session.expires_at < Time.current
signer_session.expire!
return render json: { status: "expired", error: signer_session.error }
end
if signer_session.verified?
sign_in_user(signer_session)
elsif signer_session.connected? && signer_session.user.present?
@@ -4,7 +4,7 @@ const POLL_INTERVAL = 2000
const MAX_ATTEMPTS = 160
export default class extends Controller {
static targets = [ "status", "error", "authWrapper", "authLink", "uri" ]
static targets = [ "status", "error", "authWrapper", "authLink", "uri", "details", "retry" ]
static values = { statusUrl: String }
connect () {
@@ -41,14 +41,14 @@ export default class extends Controller {
}
if (data.status === "failed" || data.status === "expired" || data.status === "disconnected") {
this.showError(data.error || "The connection failed. Please try again.")
this.showTerminalError(data.error || "The connection failed. Please try again.")
return
}
this.showAuthUrl(data.auth_url)
if (this.attempts >= MAX_ATTEMPTS) {
this.showError("Timed out waiting for your signer. Please try again.")
this.showTerminalError("Timed out waiting for your signer. Please try again.")
return
}
@@ -84,4 +84,11 @@ export default class extends Controller {
this.errorTarget.textContent = message
this.errorTarget.classList.remove("hidden")
}
showTerminalError (message) {
this.showError(message)
this.statusTarget.textContent = "Connection request ended."
if (this.hasDetailsTarget) this.detailsTarget.classList.add("hidden")
if (this.hasRetryTarget) this.retryTarget.classList.remove("hidden")
}
}
@@ -22,4 +22,30 @@ RSpec.describe NostrSignerConnectComponent, type: :component do
expect(rendered.text).to include("Connected 24 Nov 2026, 12:21")
end
end
it "renders a small, scalable QR code" do
pending_session = instance_double(
NostrSignerSession,
bunker?: false,
nostrconnect_uri: "nostrconnect://#{SecureRandom.hex(32)}?relay=wss%3A%2F%2Frelay.example.com&secret=abc"
)
rendered = render_inline(
described_class.new(
pending_session: pending_session,
create_url: "/users/nostr_connect",
status_url: "/users/nostr_connect/status",
show_disconnect: false
)
)
svg = rendered.css("svg").first
expect(svg["viewBox"]).to be_present
expect(svg["width"]).to be_nil
expect(svg["height"]).to be_nil
expect(svg["class"]).to include("w-full")
expect(rendered.css("div.w-56").any?).to be(true)
expect(rendered.css("[data-nostr-signer-connect-target='details']").any?).to be(true)
expect(rendered.text).to include("Try again")
end
end
@@ -117,6 +117,16 @@ RSpec.describe "Settings::NostrSigners", type: :request do
expect(JSON.parse(response.body)["status"]).to eq("failed")
expect(JSON.parse(response.body)["error"]).to eq("Your signer did not respond.")
end
it "expires a pending session whose TTL has passed" do
session = NostrManager::RemoteSigner::CreateSession.call(flow: "settings", user: user)
session.update_columns(expires_at: 1.minute.ago)
get nostr_signer_status_settings_path(token: session.token), as: :json
expect(JSON.parse(response.body)["status"]).to eq("expired")
expect(session.reload).to be_expired
end
end
describe "DELETE /settings/nostr_signer" do
@@ -68,6 +68,17 @@ RSpec.describe "Users::NostrConnects", type: :request do
expect(JSON.parse(response.body)["status"]).to eq("failed")
expect(signer_session.reload).to be_failed
end
it "expires a pending login session whose TTL has passed" do
get nostr_connect_path
signer_session = NostrSignerSession.last
signer_session.update_columns(expires_at: 1.minute.ago)
get nostr_connect_status_path, as: :json
expect(JSON.parse(response.body)["status"]).to eq("expired")
expect(signer_session.reload).to be_expired
end
end
describe "POST /users/nostr_connect with a bunker URI" do