import{h as i,j as e}from"./codemirror-core-DIzFijqu.js";import{k as U,b as L,W as f}from"./index-tO5nu8Yv.js";import{c as A,a as _}from"./chunk-5PILOUBS-BJiFHTkp.js";import{i as j}from"./chunk-MLPFQTYO-lAEKjCEv.js";import{i as I}from"./chunk-2QAN2V2R-CXqfx2Mo.js";import{N as E,u as T,S as W,T as v}from"./react-dom-BT-HHJs9.js";import{I as D}from"./index-COqcsWk-.js";import{l as N}from"./chunk-OPTZHMJX-sKp2mSfG.js";import{P as H,H as O,t as C,T as R}from"./pure-Devto7CM.js";import"./chunk-SLABUSGS-Kc2I43iC.js";import"./chunk-M3MASYO7-BtOvSeRL.js";import"./iconBase-ClXMXqAg.js";import"./chunk-EN4B57RQ-2RHJdNk9.js";import"./chunk-WQVQ7P2I-bQ701xkJ.js";import"./icons-DCS2NAJ5.js";import"./chunk-6LIX6LVT-BOuIs8hJ.js";function re(){const g=new URLSearchParams(window.location.search).get("token"),x=E(),[c,h]=i.useState(g||""),[o,y]=i.useState(!1),[d,P]=i.useState(!0),[,b]=T(U.token,"");function k(t){const r=t.replace(/-/g,"+").replace(/_/g,"/"),a=atob(r),s=new Uint8Array(a.length);for(let n=0;n<a.length;n++)s[n]=a.charCodeAt(n);return s}function l(t){return btoa(String.fromCharCode(...t)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}const S=async()=>{var t;try{const r=await f.generatePasskeyAuthenticationOptions(),a=await navigator.credentials.get({publicKey:{challenge:k(r.challenge),allowCredentials:(t=r.allowCredentials)==null?void 0:t.map(m=>({id:k(m.id),type:m.type,transports:m.transports})),userVerification:r.userVerification}});if(!a)throw new Error("Passkey authentication cancelled");const s=a.response,n={id:a.id,rawId:l(new Uint8Array(a.rawId)),response:{authenticatorData:l(new Uint8Array(s.authenticatorData)),clientDataJSON:l(new Uint8Array(s.clientDataJSON)),signature:l(new Uint8Array(s.signature)),userHandle:s.userHandle?l(new Uint8Array(s.userHandle)):null},type:a.type},p=await f.verifyPasskeyAuthentication(n);if(p&&p.Credential)return b(p.Credential),x("/qq_login",{replace:!0}),!0}catch(r){console.log("Passkey login failed or not available:",r)}return!1},u=async()=>{if(!c){v.error("请输入token");return}y(!0);try{const t=await f.loginWithToken(c);t&&(b(t),x("/qq_login",{replace:!0}))}catch(t){v.error(t.message)}finally{y(!1)}},w=t=>{t.key==="Enter"&&!o&&!d&&u()};return i.useEffect(()=>(document.addEventListener("keydown",w),()=>{document.removeEventListener("keydown",w)}),[c,o,d]),i.useEffect(()=>{if(g){u();return}S().finally(()=>{P(!1)})},[]),e.jsxs(e.Fragment,{children:[e.jsx("title",{children:"WebUI登录 - NapCat WebUI"}),e.jsx(H,{children:e.jsx(W.div,{initial:{opacity:0,y:20,scale:.95},animate:{opacity:1,y:0,scale:1},transition:{duration:.5,type:"spring",stiffness:120,damping:20},className:"w-[608px] max-w-full py-8 px-2 md:px-8 overflow-hidden",children:e.jsxs(O,{className:"items-center gap-4 pt-0 pb-6 bg-default-50",maxXRotation:3,maxYRotation:3,children:[e.jsxs(A,{className:"inline-block max-w-lg text-center justify-center",children:[e.jsxs("div",{className:"flex items-center justify-center w-full gap-2 pt-10",children:[e.jsx(j,{alt:"logo",height:"7em",src:N}),e.jsxs("div",{children:[e.jsx("span",{className:C(),children:"Web "}),e.jsx("span",{className:C({color:"violet"}),children:"Login "})]})]}),e.jsx(R,{className:"absolute right-4 top-4"})]}),e.jsxs(_,{className:"flex gap-5 py-5 px-5 md:px-10",children:[d&&e.jsx("div",{className:"text-center text-small text-default-600 dark:text-default-400 px-2",children:"🔐 正在检查Passkey..."}),e.jsxs("form",{onSubmit:t=>{t.preventDefault(),u()},children:[e.jsx("input",{type:"text",name:"username",value:"napcat-webui",autoComplete:"username",className:"absolute -left-[9999px] opacity-0 pointer-events-none",readOnly:!0,tabIndex:-1,"aria-label":"Username"}),e.jsx(I,{isClearable:!0,type:"password",name:"password",autoComplete:"current-password",classNames:{label:"text-black/50 dark:text-white/90",input:["bg-transparent","text-black/90 dark:text-white/90","placeholder:text-default-700/50 dark:placeholder:text-white/60"],innerWrapper:"bg-transparent",inputWrapper:["shadow-xl","bg-default-100/70","dark:bg-default/60","backdrop-blur-xl","backdrop-saturate-200","hover:bg-default-0/70","dark:hover:bg-default/70","group-data-[focus=true]:bg-default-100/50","dark:group-data-[focus=true]:bg-default/60","!cursor-text"]},isDisabled:o||d,label:"Token",placeholder:"请输入token",radius:"lg",size:"lg",startContent:e.jsx(D,{className:"text-black/50 mb-0.5 dark:text-white/90 text-slate-400 pointer-events-none flex-shrink-0"}),value:c,onChange:t=>h(t.target.value),onClear:()=>h("")})]}),e.jsx("div",{className:"text-center text-small text-default-600 dark:text-default-400 px-2",children:"💡 提示：请从 NapCat 启动日志中查看登录密钥"}),e.jsxs(L,{className:"mx-10 mt-10 text-lg py-7",color:"primary",isLoading:o,radius:"full",size:"lg",variant:"shadow",onPress:u,children:[!o&&e.jsx(j,{alt:"logo",classNames:{wrapper:"-ml-8"},height:"2em",src:N}),"登录"]})]})]})})})]})}export{re as default};
