| 1 | using AgentForge.Plugin.View; |
| 2 | |
| 3 | namespace AgentForge.Plugin.OAuth.Web.Pages; |
| 4 | |
| 5 | internal static class ForgeAuthDeviceApproveScript |
| 6 | { |
| 7 | internal static string Render(string userCodeJson) => |
| 8 | ForgeHtml.Fragment( |
| 9 | ForgeHtml.SetGlobal("__forgeUserCode", userCodeJson), |
| 10 | ForgeHtml.Script(ApproveJs)); |
| 11 | |
| 12 | private const string ApproveJs = |
| 13 | """ |
| 14 | document.getElementById('approve-form').addEventListener('submit', async (event) => { |
| 15 | event.preventDefault(); |
| 16 | const status = document.getElementById('status'); |
| 17 | const token = document.getElementById('bootstrap').value.trim(); |
| 18 | if (!token) { |
| 19 | status.className = 'error'; |
| 20 | status.textContent = 'Token required.'; |
| 21 | return; |
| 22 | } |
| 23 | status.className = 'meta'; |
| 24 | status.textContent = 'Approving…'; |
| 25 | const response = await fetch('/api/v1/auth/device/approve', { |
| 26 | method: 'POST', |
| 27 | headers: { |
| 28 | 'Content-Type': 'application/json', |
| 29 | 'Authorization': 'Bearer ' + token |
| 30 | }, |
| 31 | body: JSON.stringify({ userCode: window.__forgeUserCode }) |
| 32 | }); |
| 33 | if (response.ok) { |
| 34 | status.className = 'success'; |
| 35 | status.textContent = 'Approved. Return to the CLI — it should finish automatically.'; |
| 36 | return; |
| 37 | } |
| 38 | const payload = await response.json().catch(() => ({})); |
| 39 | status.className = 'error'; |
| 40 | status.textContent = payload.detail || payload.error || 'Approval failed.'; |
| 41 | }); |
| 42 | """; |
| 43 | } |
| 44 | |