From 095199828478b064f04370b666f2adcae43804ce Mon Sep 17 00:00:00 2001 From: Brendan Date: Wed, 16 Sep 2026 22:09:37 +0000 Subject: [PATCH] Build API keys settings page --- packages/app/src/ui.ts | 22 +++++++++++++++++++++- packages/app/test/ui.test.ts | 12 +++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/app/src/ui.ts b/packages/app/src/ui.ts index 921782fc..77b4f8ca 100644 --- a/packages/app/src/ui.ts +++ b/packages/app/src/ui.ts @@ -138,4 +138,24 @@ $('#connection').onsubmit=async e=>{e.preventDefault();const d=new FormData(e.cu const ampPayload=()=>{const d=new FormData($('#amp-connection'));return {project:d.get('project'),accessToken:d.get('accessToken'),apiBaseUrl:d.get('apiBaseUrl')}};$('#test-amp').onclick=async()=>{const m=$('#amp-message');try{const x=await request('/api/connections/amp/test',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(ampPayload())});m.textContent=x.ok?'Amp connection succeeded.':'Amp connection needs attention.'}catch(x){m.textContent=x.message}};$('#amp-connection').onsubmit=async e=>{e.preventDefault();const m=$('#amp-message');try{await request('/api/connections/amp',{method:'PUT',headers:{'content-type':'application/json'},body:JSON.stringify(ampPayload())});e.currentTarget.reset();m.textContent='Amp connection saved. The token remains write-only.';await load()}catch(x){m.textContent=x.message}};bindActions();load();`); export const apiKeysSettingsPage = (viewer: { email: string }) => shell("API Keys — Factorize", viewer, settingsLayout("api-keys", `${dashboardHead("API Keys", "Manage API keys for programmatic access to Factorize.")} -

API key management is coming soon

This page establishes the settings navigation. Key creation and management will be added in a follow-up.

`)); + +

Authorized clients

OAuth, MCP, and API clients you have allowed to access Factorize.

Loading authorized clients…
+

Access Tokens

Create scoped bearer tokens for scripts and API requests.

+ + +

Authorization header

Authorization: Bearer <token>
+
Loading access tokens…
`), `${uiHelpers} +const date=value=>value?new Intl.DateTimeFormat(undefined,{dateStyle:'medium',timeStyle:'short'}).format(new Date(value)):'Never'; +const scopes=value=>(value||[]).map(scope=>''+esc(scope)+'').join(' ')||'None'; +const accessError=message=>{const error=$('#access-error');error.textContent=message||'Could not complete the request. Try again.';error.classList.remove('hidden');error.scrollIntoView({block:'nearest'})}; +const clientRow=client=>'

'+esc(client.clientName)+'

Client '+esc(client.clientId)+'

'+scopes(client.scopes)+'
Authorized
'+date(client.authorizationDate)+'
Last used
'+date(client.lastUsedAt)+'
Authorization expires
'+date(client.expiresAt)+'
'; +const tokenRow=token=>'

'+esc(token.name)+'

'+scopes(token.scopes)+'
Expires
'+date(token.expires_at)+'
Created
'+date(token.created_at)+'
Last used
'+date(token.last_used_at)+'
'; +async function loadClients(){const loading=$('#clients-loading'),root=$('#authorized-clients'),empty=$('#clients-empty');loading.classList.remove('hidden');try{const clients=await request('/api/access/authorized-clients');root.innerHTML=clients.map(clientRow).join('');root.classList.toggle('hidden',!clients.length);empty.classList.toggle('hidden',clients.length>0);bindClientActions()}catch(error){empty.classList.remove('hidden');accessError(error.message)}finally{loading.classList.add('hidden')}} +async function loadTokens(){const loading=$('#tokens-loading'),root=$('#access-tokens'),empty=$('#tokens-empty');loading.classList.remove('hidden');try{const tokens=(await request('/api/access-tokens')).filter(token=>!token.revoked_at);root.innerHTML=tokens.map(tokenRow).join('');root.classList.toggle('hidden',!tokens.length);empty.classList.toggle('hidden',tokens.length>0);bindTokenActions()}catch(error){empty.classList.remove('hidden');accessError(error.message)}finally{loading.classList.add('hidden')}} +function bindClientActions(){document.querySelectorAll('[data-disconnect-client]').forEach(button=>button.onclick=async()=>{if(!confirm('Disconnect '+button.dataset.clientName+'? All of its tokens will stop working immediately.'))return;button.disabled=true;try{await request('/api/access/authorized-clients/'+encodeURIComponent(button.dataset.disconnectClient),{method:'DELETE'});await loadClients()}catch(error){button.disabled=false;accessError(error.message)}})} +function bindTokenActions(){document.querySelectorAll('[data-revoke-token]').forEach(button=>button.onclick=async()=>{if(!confirm('Revoke the access token “'+button.dataset.tokenName+'”? It will stop working immediately.'))return;button.disabled=true;try{await request('/api/access-tokens/'+encodeURIComponent(button.dataset.revokeToken),{method:'DELETE'});await loadTokens()}catch(error){button.disabled=false;accessError(error.message)}})} +const form=$('#token-form');$('#open-token-form').onclick=()=>{form.classList.remove('hidden');$('#open-token-form').classList.add('hidden');form.elements.name.focus()};$('#cancel-token-form').onclick=()=>{form.reset();form.classList.add('hidden');$('#open-token-form').classList.remove('hidden');$('#token-form-error').classList.add('hidden')}; +document.querySelectorAll('[data-scope-preset]').forEach(button=>button.onclick=()=>{const read=button.dataset.scopePreset==='read';form.querySelectorAll('[name=scope]').forEach(input=>input.checked=!read||input.value.endsWith(':read'))}); +form.onsubmit=async event=>{event.preventDefault();const error=$('#token-form-error'),selected=[...form.querySelectorAll('[name=scope]:checked')].map(input=>input.value);error.classList.add('hidden');if(!selected.length){error.textContent='Select at least one scope.';error.classList.remove('hidden');return}const submit=form.querySelector('button[type=submit]');submit.disabled=true;try{const result=await request('/api/access-tokens',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({name:form.elements.name.value,expiryDays:Number(form.elements.expiryDays.value),scopes:selected})});$('#new-token-value').textContent=result.token;const reveal=$('#new-token');reveal.classList.remove('hidden');reveal.focus();form.reset();form.classList.add('hidden');$('#open-token-form').classList.remove('hidden');await loadTokens()}catch(x){error.textContent=x.message;error.classList.remove('hidden')}finally{submit.disabled=false}}; +$('#copy-token').onclick=async()=>{const button=$('#copy-token');try{await navigator.clipboard.writeText($('#new-token-value').textContent);button.textContent='Copied'}catch{button.textContent='Select and copy the token'}setTimeout(()=>button.textContent='Copy',2000)}; +Promise.all([loadClients(),loadTokens()]);`); diff --git a/packages/app/test/ui.test.ts b/packages/app/test/ui.test.ts index 44799dce..94d9858d 100644 --- a/packages/app/test/ui.test.ts +++ b/packages/app/test/ui.test.ts @@ -111,7 +111,17 @@ describe("pages", () => { expect(integrations).toContain('href="/settings/integrations" aria-current="page"'); expect(integrations).toContain('href="/settings/api-keys"'); expect(apiKeys).toContain('href="/settings/api-keys" aria-current="page"'); - expect(apiKeys).toContain("API key management is coming soon"); + expect(apiKeys).toContain("Authorized clients"); + expect(apiKeys).toContain("Access Tokens"); + expect(apiKeys).toContain("Read only"); + expect(apiKeys).toContain("Read and write"); + expect(apiKeys).toContain("7 days"); + expect(apiKeys).toContain("90 days"); + expect(apiKeys).toContain("cannot be retrieved again"); + expect(apiKeys).toContain("Authorization: Bearer <token>"); + expect(apiKeys).toContain("All of its tokens will stop working immediately"); + expect(apiKeys).toContain("/api/access-tokens"); + expectInlineScriptsToParse(apiKeys); }); it("separates available providers from installed integration management", () => {