import { KeyRound, LockKeyhole, ShieldCheck, UserRound } from 'lucide-react'; import {useEffect,useState} from 'react'; import {api} from '../api'; import {Brand} from './Brand'; type Policy=Awaited>; type Pending=Awaited>; export function Login({onLogin}:{onLogin:(identifier:string,password:string,totpCode:string)=>Promise}){ const [identifier,setIdentifier]=useState(''),[password,setPassword]=useState(''),[totpCode,setTOTPCode]=useState(''),[busy,setBusy]=useState(false),[error,setError]=useState(''),[mfaRequired,setMFARequired]=useState(false); const [policy,setPolicy]=useState(),[pending,setPending]=useState(),[retry,setRetry]=useState(0); useEffect(()=>{let active=true;const complete=new URLSearchParams(window.location.search).get('oidc')==='complete';setError('');Promise.all([api.oidcStatus(),complete?api.pendingOIDC():Promise.resolve({pending:false})]).then(([status,ticket])=>{if(active){setPolicy(status);setPending(ticket);if(complete&&!ticket.pending)setError('SSO 登录请求已失效,请重新发起登录')}}).catch(()=>{if(active)setError('无法读取登录配置,请重试')});return()=>{active=false}},[retry]); async function begin(){setBusy(true);setError('');try{const r=await api.beginOIDC('login');window.location.assign(r.url)}catch(e){setError(message(e));setBusy(false)}} async function complete(action:'bind'|'create'){setBusy(true);setError('');try{await api.completeOIDC({action,identifier,password,totpCode});window.history.replaceState(null,'',window.location.pathname);window.location.reload()}catch(e){if((e as {code?:string}).code==='mfa_required')setMFARequired(true);setError(message(e));setBusy(false)}} const restricted=policy?.userPasswordLogin===false||policy?.adminPasswordLogin===false; const passwordTitle=policy?.userPasswordLogin===false?(policy.adminPasswordLogin===false?'主机账号密码登录':'管理员 / 主机密码登录'):policy?.adminPasswordLogin===false?'用户 / 主机密码登录':'登录'; return

{pending?.pending?'关联 SSO 账号':'登录控制台'}

{!policy?
{error||'正在读取登录方式…'}{error?:null}
:<> {pending?.pending?
{pending.displayName||'第三方身份已验证'}{pending.email}
:null} {policy.enabled&&!pending?.pending?<>
{restricted?'其他账号登录':'或使用密码登录'}
:null}
{e.preventDefault();if(pending?.pending){await complete('bind');return}setBusy(true);setError('');try{await onLogin(identifier,password,totpCode)}catch(e){if((e as {code?:string}).code==='mfa_required')setMFARequired(true);setError(message(e))}finally{setBusy(false)}}}> {mfaRequired?:null} {error?

{error}

:null}
{pending?.pending?
{pending.allowCreate?:null}
:null} }
} function message(e:unknown){return e instanceof Error?e.message:'登录失败,请重试'}