import { type FormEvent, useEffect, useState } from 'react'; import { Button, Card, Title } from 'animal-island-ui'; import type { ConsoleAuthDataSource, ConsoleAuthStatus } from './console-auth.js'; import { createConsoleAuthApiDataSource } from './console-auth.js'; export function ConsoleAuthSettings({ dataSource, }: { readonly dataSource?: ConsoleAuthDataSource; }) { const [source] = useState(() => dataSource ?? createConsoleAuthApiDataSource()); const [status, setStatus] = useState(); const [enabled, setEnabled] = useState(false); const [password, setPassword] = useState(''); const [confirmation, setConfirmation] = useState(''); const [saving, setSaving] = useState(false); const [notice, setNotice] = useState(''); const [error, setError] = useState(''); useEffect(() => { let active = true; void source.status().then( (next) => { if (!active) return; setStatus(next); setEnabled(next.protectionEnabled); }, () => { if (active) setError('无法读取密码保护设置。'); }, ); return () => { active = false; }; }, [source]); const save = async (event: FormEvent) => { event.preventDefault(); if (!status || saving) return; setNotice(''); setError(''); if (enabled && !status.configured) { if (password.length < 8 || !/[A-Za-z]/u.test(password) || !/\d/u.test(password)) { setError('密码至少 8 位,并同时包含字母和数字。'); return; } if (password !== confirmation) { setError('两次输入的密码不一致。'); return; } } setSaving(true); try { const input = enabled && !status.configured ? { enabled: true, newPassword: password } : { enabled }; const next = await source.update(input); setStatus(next); setEnabled(next.protectionEnabled); setPassword(''); setConfirmation(''); setNotice(next.protectionEnabled ? '密码保护已启用。' : '密码保护已关闭。'); } catch { setError('保存失败,请确认当前登录状态后重试。'); } finally { setSaving(false); } }; return (

SYSTEM SETTINGS

密码保护

可在当前管理台直接完成首次密码设置;请在可信网络中初始化并妥善保管密码。

当前部署为 HTTP,仅适用于可信内网;公网使用必须在前置代理启用 HTTPS。

参考单实例 SimAdmin 的访问方式,为整个聚合工作台增加统一登录保护。

{error && !status ?

{error}

: null} {status ? (
void save(event)}> {enabled && !status.configured ? (
setPassword(event.target.value)} /> setConfirmation(event.target.value)} /> 至少 8 位,同时包含字母和数字。密码仅保存为不可逆哈希。
) : null} {status.configured ?

访问密码已配置,不会在页面或 API 中回显。

: null} {error ?

{error}

: null} {notice ?

{notice}

: null} {status.protectionEnabled && status.authenticated ? ( ) : null}
) : !error ? (

正在读取设置…

) : null}
); }