(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[4520],{83016:(e,s,a)=>{"use strict";a.r(s),a.d(s,{default:()=>n});var t=a(95155),r=a(12115),i=a(35497),l=a(73321);function n(){let[e,s]=(0,r.useState)(""),[a,n]=(0,r.useState)(""),[d,o]=(0,r.useState)(!1),[c,u]=(0,r.useState)(null),[m,h]=(0,r.useState)("super_admin"),[x,p]=(0,r.useState)([]),[b,f]=(0,r.useState)(""),[g,j]=(0,r.useState)("password"),[v,w]=(0,r.useState)(!1),[N,y]=(0,r.useState)("Sign in with OIDC"),S=(0,l.useRouter)();(0,r.useEffect)(()=>{!async function(){let e=new AbortController,s=setTimeout(()=>e.abort(),5e3),a=window.location.origin;try{await fetch(`${a}/api/settings/require-login`,{signal:e.signal}),clearTimeout(s),u(!0)}catch(e){clearTimeout(s),u(!0)}try{let e=await fetch(`${a}/api/auth/status`,{cache:"no-store"}),s=e.ok?await e.json():{};j(s.authMode||"password"),w(!!s.oidcConfigured),y(s.oidcLoginLabel||"Sign in with OIDC")}catch{j("password"),w(!1),y("Sign in with OIDC")}}()},[S]),(0,r.useEffect)(()=>{"sub_user"===m&&fetch("/api/users").then(e=>e.ok?e.json():{users:[]}).then(e=>p(e.users||[])).catch(()=>p([]))},[m]);let C=async s=>{s.preventDefault(),o(!0),n("");try{let s=await fetch("/api/auth/login",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({password:e,loginAs:m,username:"sub_user"===m?b:void 0})});if(s.ok)S.push("/dashboard"),S.refresh();else{let e=await s.json();n(e.error||"Invalid credentials")}}catch(e){n("An error occurred. Please try again.")}finally{o(!1)}},_=v&&["oidc","both"].includes(g),k="oidc"!==g||!v;return null===c?(0,t.jsx)("div",{className:"min-h-screen flex items-center justify-center bg-bg p-4",children:(0,t.jsxs)("div",{className:"text-center",children:[(0,t.jsx)("div",{className:"inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-primary"}),(0,t.jsx)("p",{className:"text-text-muted mt-4",children:"Loading..."})]})}):(0,t.jsxs)("div",{className:"min-h-screen flex items-center justify-center bg-bg p-4 relative overflow-hidden",children:[(0,t.jsx)("div",{className:"landing-grid absolute inset-0 pointer-events-none","aria-hidden":"true"}),(0,t.jsxs)("div",{className:"relative z-10 w-full max-w-md",children:[(0,t.jsxs)("div",{className:"text-center mb-8",children:[(0,t.jsx)("h1",{className:"text-3xl font-bold text-primary mb-2",children:"openrouterx"}),(0,t.jsx)("p",{className:"text-text-muted",children:"Sign in to access the dashboard"})]}),(0,t.jsx)(i.Zp,{children:(0,t.jsxs)("form",{onSubmit:C,className:"flex flex-col gap-4",children:[_&&(0,t.jsx)(i.$n,{type:"button",variant:"primary",className:"w-full",onClick:()=>{window.location.href="/api/auth/oidc/start"},children:N}),_&&k&&(0,t.jsxs)("div",{className:"flex items-center gap-3 text-xs text-text-muted",children:[(0,t.jsx)("span",{className:"h-px flex-1 bg-border"}),(0,t.jsx)("span",{children:"or"}),(0,t.jsx)("span",{className:"h-px flex-1 bg-border"})]}),k&&(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)("div",{className:"flex gap-2",children:["super_admin","sub_user"].map(e=>(0,t.jsx)("button",{type:"button",onClick:()=>h(e),className:`flex-1 py-2 px-4 rounded-lg text-sm font-medium border transition-all ${m===e?"border-primary bg-primary/10 text-primary":"border-black/10 dark:border-white/10 text-text-muted hover:border-primary/50"}`,children:"super_admin"===e?"Super Admin":"Sub User"},e))}),"sub_user"===m?(0,t.jsxs)(t.Fragment,{children:[x.length>0?(0,t.jsxs)(i.l6,{label:"Select user",value:b,onChange:e=>f(e.target.value),required:!0,children:[(0,t.jsx)("option",{value:"",children:"— Select a user —"}),x.map(e=>(0,t.jsxs)("option",{value:e.username,children:[e.displayName," (@",e.username,")"]},e.id))]}):(0,t.jsx)(i.pd,{label:"Username",placeholder:"username",value:b,onChange:e=>f(e.target.value),required:!0}),(0,t.jsxs)("div",{className:"flex flex-col gap-2",children:[(0,t.jsx)("label",{className:"text-sm font-medium",children:"Password"}),(0,t.jsx)(i.pd,{type:"password",placeholder:"Your password",value:e,onChange:e=>s(e.target.value),required:!0})]})]}):(0,t.jsxs)("div",{className:"flex flex-col gap-2",children:[(0,t.jsx)("label",{className:"text-sm font-medium",children:"Password"}),(0,t.jsx)(i.pd,{type:"password",placeholder:"Admin password",value:e,onChange:e=>s(e.target.value),required:!0,autoFocus:!0})]}),a&&(0,t.jsx)("p",{className:"text-xs text-red-500",children:a}),(0,t.jsx)(i.$n,{type:"submit",variant:"primary",className:"w-full",loading:d,children:"Sign In"})]})]})})]})]})}},83072:(e,s,a)=>{Promise.resolve().then(a.bind(a,83016))}},e=>{e.O(0,[2574,3862,1051,1321,5497,8441,3794,7358],()=>e(e.s=83072)),_N_E=e.O()}]);