'use client'; import { useEffect, useState } from 'react'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Switch } from '@/components/ui/switch'; import services from '@/lib/services'; import type { AuthSource, AuthSourceRequest } from '@/lib/services/admin'; const emptyForm: AuthSourceRequest = { name: '', type: 'oidc', display_name: '', is_active: false, client_id: '', client_secret: '', openid_discovery_url: '', scopes: 'openid profile email', icon_url: '', }; export function AuthSourceModal({ isOpen, source, onClose, onChanged, }: { isOpen: boolean; source: AuthSource | null; onClose: () => void; onChanged: () => Promise; }) { const [form, setForm] = useState(emptyForm); const [saving, setSaving] = useState(false); useEffect(() => { if (isOpen) { if (source) { setForm({ name: source.name, type: source.type, display_name: source.display_name, is_active: source.is_active, client_id: source.client_id, client_secret: '', openid_discovery_url: source.openid_discovery_url, scopes: source.scopes || 'openid profile email', icon_url: source.icon_url, }); } else { setForm(emptyForm); } } else { setForm(emptyForm); setSaving(false); } }, [isOpen, source]); const saveSource = async () => { setSaving(true); try { if (source) { await services.adminAuthSource.updateAuthSource(source.id, form); } else { await services.adminAuthSource.createAuthSource(form); } await onChanged(); toast.success('认证源已保存'); onClose(); } catch (error) { toast.error(error instanceof Error ? error.message : '保存认证源失败'); } finally { setSaving(false); } }; return ( !open && onClose()}> {source ? '编辑认证源' : '新增认证源'} {source ? '修改系统自定义的 OIDC 认证源参数。' : '配置新的自定义 OIDC 认证源。'}
setForm((prev) => ({ ...prev, name: e.target.value })) } placeholder='例如: github' />
setForm((prev) => ({ ...prev, display_name: e.target.value })) } placeholder='例如: GitHub 登录' />
setForm((prev) => ({ ...prev, client_id: e.target.value })) } />
setForm((prev) => ({ ...prev, client_secret: e.target.value })) } placeholder={source ? '留空则保留原值' : ''} />
setForm((prev) => ({ ...prev, openid_discovery_url: e.target.value, })) } placeholder='https://...' />
setForm((prev) => ({ ...prev, scopes: e.target.value })) } />
setForm((prev) => ({ ...prev, icon_url: e.target.value })) } placeholder='https://... 或留空' />
启用认证源
启用后会立即显示在登录页和账号绑定中。
setForm((prev) => ({ ...prev, is_active: checked })) } />
); }