A platform for community discussion. Free, open, simple.
UX: Add copy button to User API Key display page
Jake Goldsborough12772446a1ce
diff --git a/app/views/user_api_keys/show.html.erb b/app/views/user_api_keys/show.html.erbindex 7d657efe09e..fdf331a13a9 100644--- a/app/views/user_api_keys/show.html.erb+++ b/app/views/user_api_keys/show.html.erb@@ -1,2 +1,23 @@<p><%= t("user_api_key.instructions", application_name: @application_name) %></p>-<code><%= @payload %></code>+<div class="user-api-key-display">+ <code id="user-api-key-payload"><%= @payload %></code>+</div>+<div>+ <button class="btn btn-primary" id="copy-api-key-btn">+ <%= t("user_api_key.copy_key") %>+ </button>+</div>++<script nonce="<%= csp_nonce_placeholder %>">+ document.getElementById('copy-api-key-btn').addEventListener('click', function() {+ const btn = this;+ const payload = document.getElementById('user-api-key-payload').textContent.replace(/\s/g, '');+ navigator.clipboard.writeText(payload).then(() => {+ const originalText = btn.textContent;+ btn.textContent = '<%= t("user_api_key.copied") %>';+ setTimeout(() => {+ btn.textContent = originalText;+ }, 2000);+ });+ });+</script>diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 9e8ea778f17..57730937143 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1263,6 +1263,8 @@ en:permissions_header: 'This will allow "%{application_name}" to:'redirect_warning: "Authorizing will redirect you to"instructions: 'We just generated a new user API key for you to use with "%{application_name}", please paste the following key into your application:'+ copy_key: "Copy Key"+ copied: "Copied!"otp_description: 'Would you like to allow "%{application_name}" to access this site?'otp_confirmation:confirm_title: Continue to %{site_name}diff --git a/spec/system/user_api_key_show_spec.rb b/spec/system/user_api_key_show_spec.rbnew file mode 100644index 00000000000..5f5c7ce97ad--- /dev/null+++ b/spec/system/user_api_key_show_spec.rb@@ -0,0 +1,43 @@+# frozen_string_literal: true++RSpec.describe "User API Key Show Page", type: :system do+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }+ let(:cdp) { PageObjects::CDP.new }++ let(:public_key) { OpenSSL::PKey::RSA.new(2048).public_key.to_pem }++ before { SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_0] }++ it "displays the copy button and copies the payload without whitespace" do+ sign_in(user)++ # Visit the authorization page (no auth_redirect means show page after submit)+ visit "/user-api-key/new?#{URI.encode_www_form(scopes: "read", client_id: "x" * 32, application_name: "Test Application", public_key: public_key, nonce: SecureRandom.hex)}"++ # Submit the authorization form+ click_button I18n.t("user_api_key.authorize")++ # Now we should be on the show page+ expect(page).to have_css("#user-api-key-payload")+ expect(page).to have_css("#copy-api-key-btn")++ cdp.allow_clipboard++ displayed_payload = find("#user-api-key-payload").text++ # The displayed payload should have whitespace (rendered from newlines in Base64.encode64)+ expect(displayed_payload).to match(/\s/)++ find("#copy-api-key-btn").click++ # Verify button text changes to "Copied"+ expect(page).to have_button(I18n.t("user_api_key.copied"))++ # Verify clipboard contains payload without whitespace+ clipboard_content = cdp.read_clipboard+ expect(clipboard_content).not_to match(/\s/)++ # Verify the copied content can be base64 decoded+ expect { Base64.decode64(clipboard_content) }.not_to raise_error+ end+end