'use client'; import { appPath } from '@/lib/base-path'; import { useState, type FormEvent } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { ArrowLeft, CircleAlert, Eye, EyeOff, Info, LoaderCircle, LogOut } from 'lucide-react'; export function PasswordForm({ username, mustChangePassword, passwordSet = true }: { username: string; mustChangePassword: boolean; passwordSet?: boolean }) { const router = useRouter(); const [visible, setVisible] = useState(false); const [busy, setBusy] = useState(false); const [leaving, setLeaving] = useState(false); const [error, setError] = useState(''); async function submit(event: FormEvent) { event.preventDefault(); const fields = new FormData(event.currentTarget); setError(''); if (fields.get('newPassword') !== fields.get('confirmation')) { setError('两次输入的新密码不一致,请重新确认。'); event.currentTarget.querySelector('#confirmation')?.focus(); return; } setBusy(true); try { const response = await fetch(appPath('/api/auth/password'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...(passwordSet ? { currentPassword: fields.get('currentPassword') } : {}), newPassword: fields.get('newPassword') }), }); const data = await response.json() as { error?: string }; if (!response.ok) throw new Error(data.error || '修改失败,请稍后重试。'); router.replace('/'); router.refresh(); } catch (cause) { setError(cause instanceof Error ? cause.message : '无法连接服务,请检查网络后重试。'); setBusy(false); } } async function logout() { setLeaving(true); setError(''); try { const response = await fetch(appPath('/api/auth/logout'), { method: 'POST' }); if (!response.ok) throw new Error('退出登录失败,请重试。'); router.replace('/login'); router.refresh(); } catch (cause) { setError(cause instanceof Error ? cause.message : '退出登录失败,请重试。'); setLeaving(false); } } return <> {!passwordSet &&
你当前通过飞书登录。设置系统密码后,也可使用上方账号和密码登录。
} {mustChangePassword &&
请将管理员分配的临时密码改为自己的密码,完成后即可进入工作台。
}
{error &&
{error}
}
{passwordSet && }
12–128 个字符,可使用方便记忆的长密码。
{!mustChangePassword && 返回工作台}
; }