feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { KeyRound, LockKeyhole, ShieldCheck, UserRound } from 'lucide-react';
|
||||
import {useEffect,useState} from 'react';
|
||||
import {api} from '../api';
|
||||
import {Brand} from './Brand';
|
||||
|
||||
type Policy=Awaited<ReturnType<typeof api.oidcStatus>>;
|
||||
type Pending=Awaited<ReturnType<typeof api.pendingOIDC>>;
|
||||
export function Login({onLogin}:{onLogin:(identifier:string,password:string,totpCode:string)=>Promise<void>}){
|
||||
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<Policy>(),[pending,setPending]=useState<Pending>(),[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 <main className="login-page"><section className="login-panel"><Brand/><div className="login-copy"><h1>{pending?.pending?'关联 SSO 账号':'登录控制台'}</h1></div>
|
||||
{!policy?<div className="login-loading" role="status">{error||'正在读取登录方式…'}{error?<button className="secondary" onClick={()=>setRetry(v=>v+1)}>重新读取</button>:null}</div>:<>
|
||||
{pending?.pending?<div className="sso-identity"><KeyRound size={20}/><span><b>{pending.displayName||'第三方身份已验证'}</b><small>{pending.email}</small></span></div>:null}
|
||||
{policy.enabled&&!pending?.pending?<><button className="primary oidc-login" disabled={busy} onClick={()=>void begin()}><KeyRound size={17}/>SSO 账号登录</button><div className="login-divider"><span>{restricted?'其他账号登录':'或使用密码登录'}</span></div></>:null}
|
||||
<form onSubmit={async e=>{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)}}}>
|
||||
<label><span>{pending?.pending?'已有用户或管理员账号':'账号或主机名'}</span><div><UserRound size={17}/><input required autoComplete="username" value={identifier} onChange={e=>{setIdentifier(e.target.value);setMFARequired(false);setTOTPCode('')}}/></div></label>
|
||||
<label><span>密码</span><div><LockKeyhole size={17}/><input type="password" required autoComplete="current-password" value={password} onChange={e=>{setPassword(e.target.value);setMFARequired(false);setTOTPCode('')}}/></div></label>
|
||||
{mfaRequired?<label><span>动态验证码或恢复码</span><div><ShieldCheck size={17}/><input autoFocus required autoComplete="one-time-code" value={totpCode} onChange={e=>setTOTPCode(e.target.value)} placeholder="6 位验证码"/></div></label>:null}
|
||||
{error?<p className="form-error" role="alert">{error}</p>:null}
|
||||
<button className={pending?.pending?'secondary':'primary'} disabled={busy}>{busy?'正在处理…':pending?.pending?'绑定已有账号并登录':mfaRequired?'验证并登录':passwordTitle}</button></form>
|
||||
{pending?.pending?<div className="sso-create-actions">{pending.allowCreate?<button className="primary" disabled={busy} onClick={()=>void complete('create')}>创建普通用户并登录</button>:null}<button className="text-button" disabled={busy} onClick={()=>void begin()}>更换 SSO 账号</button></div>:null}
|
||||
</>}</section></main>
|
||||
}
|
||||
function message(e:unknown){return e instanceof Error?e.message:'登录失败,请重试'}
|
||||
Reference in new issue
Block a user