| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"><head><meta charset="utf-8"> |
| 3 | <meta name="viewport" content="width=device-width,initial-scale=1"> |
| 4 | <title>opencode</title> |
| 5 | <!--TOKENS--> |
| 6 | <style> |
| 7 | *{box-sizing:border-box} |
| 8 | html,body{margin:0;min-height:100%} |
| 9 | body{background:var(--bg);color:var(--text);font-family:var(--font);font-size:14px; |
| 10 | line-height:1.5;-webkit-font-smoothing:antialiased} |
| 11 | #app{max-width:840px;margin:0 auto;padding:16px 16px 40px} |
| 12 | .hd{display:flex;align-items:center;gap:10px;flex-wrap:wrap;margin-bottom:14px} |
| 13 | .hd .grow{flex:1 1 auto;min-width:0} |
| 14 | .hd h1{margin:0;font-size:18px;font-weight:650;letter-spacing:-.01em} |
| 15 | .hd .sub{font-size:11.5px;color:var(--text-2);margin-top:2px;white-space:nowrap; |
| 16 | overflow:hidden;text-overflow:ellipsis} |
| 17 | .pill{display:inline-flex;align-items:center;gap:6px;font-size:11.5px;font-weight:650; |
| 18 | padding:4px 10px;border-radius:999px;background:var(--surface-2);color:var(--text-2); |
| 19 | border:1px solid var(--line);white-space:nowrap} |
| 20 | .pill .dot{width:7px;height:7px;border-radius:50%;background:var(--gray);flex:0 0 auto} |
| 21 | .pill.ok{color:var(--green);border-color:rgba(48,209,88,.35)} |
| 22 | .pill.ok .dot{background:var(--green);box-shadow:0 0 0 3px rgba(48,209,88,.15)} |
| 23 | .pill.warn{color:var(--orange);border-color:rgba(255,159,10,.35)} |
| 24 | .pill.warn .dot{background:var(--orange)} |
| 25 | .pill.off .dot{background:var(--gray)} |
| 26 | .badge{display:inline-flex;align-items:center;gap:6px;font-size:11.5px;font-weight:600; |
| 27 | padding:4px 10px;border-radius:999px;background:var(--surface-2);color:var(--text); |
| 28 | border:1px solid var(--line);white-space:nowrap;max-width:220px;overflow:hidden; |
| 29 | text-overflow:ellipsis} |
| 30 | .badge .av{width:18px;height:18px;border-radius:50%;background:var(--blue);color:#fff; |
| 31 | display:flex;align-items:center;justify-content:center;font-size:10px;font-weight:700; |
| 32 | flex:0 0 auto} |
| 33 | .card{background:var(--bg-card);border:1px solid var(--line);border-radius:14px; |
| 34 | padding:16px;margin-bottom:12px} |
| 35 | .card h2{margin:0 0 4px;font-size:13px;font-weight:700;letter-spacing:.08em; |
| 36 | text-transform:uppercase;color:var(--text-2)} |
| 37 | .card .lead{margin:0 0 12px;font-size:12.5px;color:var(--text-2)} |
| 38 | .row{display:flex;align-items:center;gap:8px;flex-wrap:wrap} |
| 39 | .btn{background:var(--blue);color:#fff;border:0;border-radius:980px;padding:7px 16px; |
| 40 | font-weight:600;font-size:13px;cursor:pointer;font-family:inherit; |
| 41 | transition:transform .12s var(--ease),opacity .12s} |
| 42 | .btn:active{transform:scale(.98)} |
| 43 | .btn:disabled{opacity:.5;cursor:default} |
| 44 | .btn.ghost{background:var(--surface-2);color:var(--text);border:1px solid var(--line)} |
| 45 | .btn.ghost:hover{background:var(--surface-hover);border-color:var(--line-2)} |
| 46 | .btn.danger{background:transparent;color:var(--red);border:1px solid rgba(255,69,58,.35)} |
| 47 | a.btn{display:inline-flex;align-items:center;gap:6px;text-decoration:none} |
| 48 | .kv{display:grid;grid-template-columns:132px minmax(0,1fr);gap:6px 12px;font-size:13px} |
| 49 | .kv .k{color:var(--text-2);font-size:12px} |
| 50 | .kv .v{font-family:var(--mono);font-size:12px;word-break:break-all} |
| 51 | .kv .v.plain{font-family:var(--font);font-size:13px} |
| 52 | .hint{font-size:12px;color:var(--text-2);margin:10px 0 0} |
| 53 | .step{display:none} |
| 54 | .step.on{display:block} |
| 55 | .codebox{margin:10px 0;padding:14px 16px;border-radius:12px;background:var(--code-bg); |
| 56 | border:1px solid var(--line);text-align:center} |
| 57 | .codebox .code{font-family:var(--mono);font-size:26px;font-weight:700;letter-spacing:.12em; |
| 58 | color:var(--text);margin-bottom:6px} |
| 59 | .codebox .code-empty{font-family:var(--mono);font-size:18px;color:var(--text-2); |
| 60 | letter-spacing:.08em} |
| 61 | .pollbar{margin:10px 0 0;height:3px;border-radius:2px;background:var(--surface-2); |
| 62 | overflow:hidden} |
| 63 | .pollbar i{display:block;height:100%;width:0;background:var(--blue); |
| 64 | animation:pollslide 2s linear infinite} |
| 65 | @keyframes pollslide{0%{width:0}50%{width:100%}100%{width:0}} |
| 66 | .polllbl{font-size:11.5px;color:var(--text-2);margin:8px 0 0;display:flex;align-items:center;gap:6px} |
| 67 | .polllbl .spin{width:10px;height:10px;border-radius:50%;border:2px solid var(--line-2); |
| 68 | border-top-color:var(--blue);animation:spin 1s linear infinite;display:inline-block} |
| 69 | @keyframes spin{to{transform:rotate(360deg)}} |
| 70 | .authurl{margin:10px 0;padding:10px 11px;border-radius:10px;background:var(--code-bg); |
| 71 | border:1px solid var(--line);font-family:var(--mono);font-size:11.5px; |
| 72 | word-break:break-all;max-height:120px;overflow:auto;color:var(--text)} |
| 73 | .note.warn{color:var(--orange);border-color:rgba(255,159,10,.35)} |
| 74 | #themeToggle{background:var(--surface-2);border:1px solid var(--line);color:var(--text); |
| 75 | border-radius:980px;padding:5px 12px;font-size:12px;font-weight:600;cursor:pointer; |
| 76 | font-family:inherit} |
| 77 | .note{display:none;margin-top:10px;padding:9px 11px;border-radius:10px;font-size:12.5px; |
| 78 | border:1px solid var(--line);background:var(--surface-2)} |
| 79 | .note.show{display:block} |
| 80 | .note.err{color:var(--red);border-color:rgba(255,69,58,.35)} |
| 81 | .note.ok{color:var(--green);border-color:rgba(48,209,88,.35)} |
| 82 | .empty{padding:10px 0;color:var(--text-2);font-size:12.5px} |
| 83 | .tok{font-size:11.5px;color:var(--text-2);margin-top:8px} |
| 84 | .tok .ok{color:var(--green)} |
| 85 | .tok .no{color:var(--red)} |
| 86 | footer{font-size:11px;color:var(--text-2);margin-top:24px;text-align:center} |
| 87 | footer a{color:var(--text-2)} |
| 88 | @media(max-width:480px){ |
| 89 | #app{padding:12px 12px 32px} |
| 90 | .hd h1{font-size:16px} |
| 91 | .kv{grid-template-columns:110px minmax(0,1fr)} |
| 92 | .codebox .code{font-size:22px} |
| 93 | } |
| 94 | </style> |
| 95 | </head> |
| 96 | <body> |
| 97 | <div id="app"> |
| 98 | <div class="hd"> |
| 99 | <div class="grow"> |
| 100 | <h1>OpenCode Login</h1> |
| 101 | <div class="sub">Device code login and account token manager for this node</div> |
| 102 | </div> |
| 103 | <span class="badge" id="userBadge" hidden> |
| 104 | <span class="av" id="userAv">?</span><span id="userLabel"></span> |
| 105 | </span> |
| 106 | <span class="pill off" id="pill"><span class="dot"></span><span id="pillText">Checking</span></span> |
| 107 | <button class="btn ghost embed-hide" id="themeToggle" type="button" title="Toggle appearance">Theme</button> |
| 108 | </div> |
| 109 | |
| 110 | <!-- Session card --> |
| 111 | <div class="card"> |
| 112 | <h2>Session</h2> |
| 113 | <p class="lead">The plugin reads the credential file written by the node's own login CLI. It never sees, stores, or returns a token value.</p> |
| 114 | <div class="kv" id="sessionKv"> |
| 115 | <div class="k">Status</div><div class="v plain" id="vStatus">Checking</div> |
| 116 | <div class="k">Account</div><div class="v plain" id="vAccount">-</div> |
| 117 | <div class="k">Token</div><div class="v plain" id="vToken">-</div> |
| 118 | <div class="k">Refresh</div><div class="v plain" id="vRefresh">-</div> |
| 119 | <div class="k">Profile</div><div class="v plain" id="vProfile">-</div> |
| 120 | <div class="k">Credentials</div><div class="v" id="vPath">-</div> |
| 121 | </div> |
| 122 | <div class="tok" id="tokLine"></div> |
| 123 | <div class="row" style="margin-top:12px"> |
| 124 | <button class="btn" id="btnRefresh" type="button">Refresh Token Now</button> |
| 125 | <button class="btn ghost" id="btnStartLogin" type="button">Start OpenCode Login</button> |
| 126 | <button class="btn ghost" id="btnRelogin" type="button" hidden>Re-login (replace)</button> |
| 127 | </div> |
| 128 | <div class="note" id="sessionNote"></div> |
| 129 | </div> |
| 130 | |
| 131 | <!-- Device flow card --> |
| 132 | <div class="card" id="flowCard"> |
| 133 | <h2>Device Flow</h2> |
| 134 | <p class="lead">OpenCode signs in with a one-time user code. Copy it, open the link, approve, and this panel updates itself.</p> |
| 135 | |
| 136 | <div class="step on" id="stepIdle"> |
| 137 | <div class="empty">No login in progress. Press <b>Start OpenCode Login</b> above to begin.</div> |
| 138 | </div> |
| 139 | |
| 140 | <div class="step" id="stepActive"> |
| 141 | <div class="codebox"> |
| 142 | <div class="code" id="userCode">--</div> |
| 143 | <div class="row" style="justify-content:center"> |
| 144 | <button class="btn" id="btnCopyOpen" type="button">Copy Code & Open OpenCode</button> |
| 145 | </div> |
| 146 | </div> |
| 147 | <div class="authurl" id="verifyUrl"></div> |
| 148 | <div class="pollbar"><i></i></div> |
| 149 | <div class="polllbl"><span class="spin"></span><span id="pollLabel">Waiting for approval</span></div> |
| 150 | <div class="row" style="margin-top:12px"> |
| 151 | <button class="btn danger" id="btnCancel" type="button">Cancel</button> |
| 152 | </div> |
| 153 | <div class="note" id="flowNote"></div> |
| 154 | </div> |
| 155 | </div> |
| 156 | |
| 157 | <footer> |
| 158 | <span id="footInfo">plugin_opencode_login</span> |
| 159 | · <a href="https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-opencode-login">source</a> |
| 160 | </footer> |
| 161 | </div> |
| 162 | <script> |
| 163 | (function(){ |
| 164 | var q=new URLSearchParams(location.search); |
| 165 | var node=q.get('node')||''; |
| 166 | var sid=q.get('id')||''; |
| 167 | var HUB=q.get('plugin')||''; |
| 168 | var pwd=q.get('pwd')||(node&&localStorage.getItem('bot_pwd_'+node))||localStorage.getItem('bot_active_pwd')||''; |
| 169 | var POLL_MS=5000; |
| 170 | var pollTimer=null; |
| 171 | var flowActive=false; |
| 172 | var flowVerify=''; |
| 173 | var flowCode=''; |
| 174 | |
| 175 | function esc(s){return String(s==null?'':s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');} |
| 176 | function $(id){return document.getElementById(id);} |
| 177 | function setPill(cls,text){var p=$('pill');p.className='pill '+cls;$('pillText').textContent=text;} |
| 178 | function note(id,cls,text){var n=$(id);if(!n)return;n.className='note '+(cls||'')+(text?' show':'');n.textContent=text||'';} |
| 179 | |
| 180 | function api(action,extra){ |
| 181 | var u; |
| 182 | if(HUB) u='/plugins?plugin='+encodeURIComponent(HUB)+'&action='+encodeURIComponent(action); |
| 183 | else u='/plugin_opencode_login?action='+encodeURIComponent(action); |
| 184 | if(extra) u+='&'+extra; |
| 185 | if(node) u+='&node='+encodeURIComponent(node); |
| 186 | if(sid) u+='&id='+encodeURIComponent(sid); |
| 187 | if(pwd) u+='&pwd='+encodeURIComponent(pwd); |
| 188 | return fetch(u,{headers:pwd?{'X-Bot-Password':pwd}:{}}).then(function(r){return r.json();}); |
| 189 | } |
| 190 | |
| 191 | function fmtExpiry(sec){ |
| 192 | if(sec==null||sec<=0)return 'unknown'; |
| 193 | if(sec<60)return sec+'s'; |
| 194 | var m=Math.floor(sec/60); |
| 195 | if(m<60)return m+' min'; |
| 196 | var h=Math.floor(m/60); |
| 197 | if(h<48)return h+' h '+(m%60)+' min'; |
| 198 | var d=Math.floor(h/24); |
| 199 | return d+' days'; |
| 200 | } |
| 201 | |
| 202 | function renderStatus(d){ |
| 203 | if(!d||d.ok===false){setPill('off','Not Connected');note('sessionNote','err',d&&d.error?('Error: '+d.error):'No response');return;} |
| 204 | var li=!!d.logged_in, ex=!!d.expired, pend=!!d.pending; |
| 205 | if(li&&!ex){setPill('ok','Connected');} |
| 206 | else if(li&&ex){setPill('warn','Expired');} |
| 207 | else if(pend){setPill('warn','Signing in');} |
| 208 | else{setPill('off','Not Connected');} |
| 209 | |
| 210 | $('vStatus').textContent=li?(ex?'Signed in (token expired)':'Signed in'):(pend?'Device flow in progress':'Not signed in'); |
| 211 | $('vAccount').textContent=d.account||d.username||'-'; |
| 212 | $('vToken').textContent=li?(ex?'expired ('+fmtExpiry(d.expires_in_sec)+')':'valid, expires in '+fmtExpiry(d.expires_in_sec)):'none'; |
| 213 | $('vRefresh').textContent=d.has_refresh?'stored (presence only)':'not stored'; |
| 214 | $('vProfile').textContent=d.profile_exists?(d.profile||'superopencode')+' (exists)':'not created'; |
| 215 | $('vPath').textContent=d.path||'-'; |
| 216 | |
| 217 | var tok=$('tokLine'); |
| 218 | if(!d.cli_present){tok.innerHTML='<span class="no">scorpiox-opencode-login is not on this node.</span>';} |
| 219 | else{tok.innerHTML='<span class="ok">Login CLI present</span> · token CLI '+(d.token_cli_present?'present':'missing');} |
| 220 | |
| 221 | var badge=$('userBadge'); |
| 222 | if(li){badge.hidden=false;var lbl=d.account||d.username||'OpenCode';$('userLabel').textContent=lbl; |
| 223 | $('userAv').textContent=(lbl.charAt(0)||'O').toUpperCase();} |
| 224 | else{badge.hidden=true;} |
| 225 | |
| 226 | $('btnRelogin').hidden=!li; |
| 227 | $('btnStartLogin').disabled=pend; |
| 228 | |
| 229 | if(pend&&!flowActive){startPollingUI();} |
| 230 | if(!pend&&flowActive){stopPollingUI();$('stepIdle').classList.add('on');$('stepActive').classList.remove('on');} |
| 231 | if(!li&&!pend){note('sessionNote','ok','No session on this node yet. Start a device login below.');} |
| 232 | else if(li&&!ex){note('sessionNote','ok','');} |
| 233 | else if(li&&ex){note('sessionNote','err','Token expired. Re-login to refresh the session.');} |
| 234 | } |
| 235 | |
| 236 | function refresh(){api('status').then(renderStatus).catch(function(e){setPill('off','Not Connected');note('sessionNote','err','Request failed: '+e);});} |
| 237 | |
| 238 | function startPollingUI(){ |
| 239 | flowActive=true; |
| 240 | $('stepIdle').classList.remove('on'); |
| 241 | $('stepActive').classList.add('on'); |
| 242 | if(!pollTimer){pollTimer=setInterval(pollOnce,POLL_MS);} |
| 243 | } |
| 244 | function stopPollingUI(){ |
| 245 | flowActive=false; |
| 246 | if(pollTimer){clearInterval(pollTimer);pollTimer=null;} |
| 247 | } |
| 248 | |
| 249 | function pollOnce(){ |
| 250 | if(!flowActive)return; |
| 251 | api('poll').then(function(d){ |
| 252 | if(!d||d.ok===false){note('flowNote','err',d&&d.error?('Poll error: '+d.error):'Poll failed');return;} |
| 253 | if(d.state==='done'||d.logged_in){ |
| 254 | stopPollingUI(); |
| 255 | $('stepIdle').classList.add('on'); |
| 256 | $('stepActive').classList.remove('on'); |
| 257 | note('flowNote','ok','Signed in. The credential file has been written.'); |
| 258 | $('pollLabel').textContent='Signed in'; |
| 259 | refresh(); |
| 260 | }else if(d.state==='failed'){ |
| 261 | stopPollingUI(); |
| 262 | $('stepIdle').classList.add('on'); |
| 263 | $('stepActive').classList.remove('on'); |
| 264 | note('flowNote','err','Login failed: '+(d.error||'unknown')+(d.detail?(' - '+d.detail):'')); |
| 265 | }else{ |
| 266 | $('pollLabel').textContent='Waiting for approval'; |
| 267 | } |
| 268 | }).catch(function(e){note('flowNote','err','Poll request failed: '+e);}); |
| 269 | } |
| 270 | |
| 271 | function doStart(force){ |
| 272 | note('sessionNote','ok',''); |
| 273 | api('start',force?'force=1':'').then(function(d){ |
| 274 | if(!d||d.ok===false){note('sessionNote','err',d&&d.error?('Start failed: '+d.error):'Start failed');return;} |
| 275 | if(d.already_logged_in){ |
| 276 | note('sessionNote','warn','This node already has OpenCode credentials. Use Re-login to replace them.'); |
| 277 | refresh(); |
| 278 | return; |
| 279 | } |
| 280 | flowVerify=d.verify_url||''; |
| 281 | flowCode=d.user_code||''; |
| 282 | $('userCode').textContent=flowCode||'--'; |
| 283 | $('verifyUrl').textContent=flowVerify; |
| 284 | $('pollLabel').textContent='Waiting for approval'; |
| 285 | note('flowNote','ok',''); |
| 286 | startPollingUI(); |
| 287 | refresh(); |
| 288 | }).catch(function(e){note('sessionNote','err','Start request failed: '+e);}); |
| 289 | } |
| 290 | |
| 291 | $('btnStartLogin').onclick=function(){doStart(false);}; |
| 292 | $('btnRelogin').onclick=function(){doStart(true);}; |
| 293 | $('btnCancel').onclick=function(){ |
| 294 | stopPollingUI(); |
| 295 | api('cancel').then(function(d){ |
| 296 | $('stepIdle').classList.add('on'); |
| 297 | $('stepActive').classList.remove('on'); |
| 298 | note('flowNote','ok',d&&d.ok?'Device flow cancelled.':'Cancel failed: '+(d&&d.error||'unknown')); |
| 299 | refresh(); |
| 300 | }).catch(function(e){note('flowNote','err','Cancel request failed: '+e);}); |
| 301 | }; |
| 302 | $('btnRefresh').onclick=function(){ |
| 303 | note('sessionNote','ok',''); |
| 304 | api('refresh').then(function(d){ |
| 305 | if(!d||d.ok===false){note('sessionNote','err',d&&d.error?('Refresh failed: '+d.error):'Refresh failed');return;} |
| 306 | note('sessionNote','ok','Token re-read. '+(d.refreshed?'CLI confirmed a live token.':'')); |
| 307 | renderStatus(d); |
| 308 | }).catch(function(e){note('sessionNote','err','Refresh request failed: '+e);}); |
| 309 | }; |
| 310 | $('btnCopyOpen').onclick=function(){ |
| 311 | var done=function(){note('flowNote','ok','Code copied. Open the link and approve.');}; |
| 312 | if(navigator.clipboard&&navigator.clipboard.writeText){navigator.clipboard.writeText(flowCode).then(done,function(){window.open(flowVerify,'_blank');done();});} |
| 313 | else{done();} |
| 314 | if(flowVerify)window.open(flowVerify,'_blank'); |
| 315 | }; |
| 316 | |
| 317 | var tb=$('themeToggle'); |
| 318 | if(tb)tb.onclick=function(){ |
| 319 | if(window.sxApplyTheme)window.sxApplyTheme(window.sxTheme()==='dark'?'light':'dark'); |
| 320 | }; |
| 321 | $('footInfo').textContent='plugin_opencode_login'; |
| 322 | refresh(); |
| 323 | })(); |
| 324 | </script> |
| 325 | </body></html> |