feat(auth): 新增手机号与 DCAuth 设备认证登录
- 登录链路改为用户名 + 密码 + 短信验证码 + DCAuth v2 设备签名 - 新增 login_sms_codes / user_devices / device_challenges 三张表,迁移 0063/0064 - 设备绑定只比对 deviceId + publicKey,每账号一个 active 设备,管理员可重置 - 保留未绑定手机号账号的密码登录兜底,绑定后强制安全登录 - 登录页与用户管理页支持验证码、DCAuth 轮询、手机号绑定与设备重置
This commit is contained in:
@@ -5,7 +5,7 @@ import {
|
||||
} from 'antd';
|
||||
import type { TableProps } from 'antd';
|
||||
import { message } from '../utils/antdMessage';
|
||||
import { PlusOutlined, EditOutlined, DeleteOutlined, SafetyOutlined } from '@ant-design/icons';
|
||||
import { PlusOutlined, EditOutlined, DeleteOutlined, SafetyOutlined, ReloadOutlined } from '@ant-design/icons';
|
||||
import { userApi, type UserInfo } from '../api/modules';
|
||||
import { usePermissions } from '../hooks/usePermissions';
|
||||
import { getErrorMessage } from '../utils/error';
|
||||
@@ -59,6 +59,13 @@ export default function UsersPage() {
|
||||
const [selectedPerms, setSelectedPerms] = useState<string[]>([]);
|
||||
const [useCustom, setUseCustom] = useState(false);
|
||||
const [permLoading, setPermLoading] = useState(false);
|
||||
|
||||
// 手机号绑定 / 设备重置
|
||||
const [phoneModalOpen, setPhoneModalOpen] = useState(false);
|
||||
const [phoneUser, setPhoneUser] = useState<UserInfo | null>(null);
|
||||
const [phoneForm] = Form.useForm<{ phone: string }>();
|
||||
const [phoneSaving, setPhoneSaving] = useState(false);
|
||||
const [resettingId, setResettingId] = useState<number | null>(null);
|
||||
const { can } = usePermissions();
|
||||
|
||||
const canAssignPerm = can('user:assign_permissions');
|
||||
@@ -136,6 +143,42 @@ export default function UsersPage() {
|
||||
}
|
||||
};
|
||||
|
||||
// ---- 手机号 / 设备 ----
|
||||
const handlePhoneClick = (user: UserInfo) => {
|
||||
setPhoneUser(user);
|
||||
phoneForm.setFieldsValue({ phone: user.phone || '' });
|
||||
setPhoneModalOpen(true);
|
||||
};
|
||||
|
||||
const handlePhoneSave = async () => {
|
||||
if (!phoneUser) return;
|
||||
try {
|
||||
const values = await phoneForm.validateFields();
|
||||
setPhoneSaving(true);
|
||||
await userApi.bindPhone(phoneUser.id, values.phone.trim());
|
||||
message.success('手机号已更新');
|
||||
setPhoneModalOpen(false);
|
||||
loadUsers();
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e));
|
||||
} finally {
|
||||
setPhoneSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleResetDevice = async (id: number) => {
|
||||
setResettingId(id);
|
||||
try {
|
||||
const result = await userApi.resetDevice(id);
|
||||
message.success(result.message || (result.reset ? '已重置设备' : '当前没有已绑定设备'));
|
||||
loadUsers();
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e));
|
||||
} finally {
|
||||
setResettingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
// ---- 权限管理 ----
|
||||
const handlePermClick = async (user: UserInfo) => {
|
||||
setPermUser(user);
|
||||
@@ -209,6 +252,55 @@ export default function UsersPage() {
|
||||
width: 80,
|
||||
render: (active: boolean) => active ? <Tag color="green">启用</Tag> : <Tag>禁用</Tag>,
|
||||
},
|
||||
{
|
||||
title: '手机号',
|
||||
dataIndex: 'phone',
|
||||
width: 170,
|
||||
render: (_: unknown, record: UserInfo) => (
|
||||
<Space size={4}>
|
||||
{record.phone ? (
|
||||
<span>{record.phone_masked || record.phone}</span>
|
||||
) : (
|
||||
<Tag>未绑定</Tag>
|
||||
)}
|
||||
{canEdit && (
|
||||
<Button size="small" type="link" onClick={() => handlePhoneClick(record)}>
|
||||
{record.phone ? '修改' : '绑定'}
|
||||
</Button>
|
||||
)}
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: '绑定设备',
|
||||
dataIndex: 'device_id',
|
||||
width: 230,
|
||||
render: (_: unknown, record: UserInfo) =>
|
||||
record.device_id ? (
|
||||
<Space size={4} wrap>
|
||||
<span style={{ fontFamily: 'monospace' }}>{record.device_id_masked || record.device_id}</span>
|
||||
{canEdit && (
|
||||
<Popconfirm
|
||||
title="确认重置设备?"
|
||||
description="重置后该账号下次登录会重新绑定当前设备。"
|
||||
onConfirm={() => handleResetDevice(record.id)}
|
||||
>
|
||||
<Button
|
||||
size="small"
|
||||
type="link"
|
||||
danger
|
||||
icon={<ReloadOutlined />}
|
||||
loading={resettingId === record.id}
|
||||
>
|
||||
重置设备
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
)}
|
||||
</Space>
|
||||
) : (
|
||||
<Tag>未绑定</Tag>
|
||||
),
|
||||
},
|
||||
{ title: '备注', dataIndex: 'remark', width: 180, ellipsis: true },
|
||||
{
|
||||
title: '操作',
|
||||
@@ -250,7 +342,7 @@ export default function UsersPage() {
|
||||
loading={loading}
|
||||
size="small"
|
||||
tableLayout="fixed"
|
||||
scroll={{ x: 844 }}
|
||||
scroll={{ x: 1240 }}
|
||||
pagination={{
|
||||
current: currentPage,
|
||||
pageSize,
|
||||
@@ -378,6 +470,35 @@ export default function UsersPage() {
|
||||
</>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
{/* 绑定 / 修改手机号 Modal */}
|
||||
<Modal
|
||||
title={phoneUser ? `绑定手机号 - ${phoneUser.username}` : '绑定手机号'}
|
||||
open={phoneModalOpen}
|
||||
onCancel={() => setPhoneModalOpen(false)}
|
||||
onOk={handlePhoneSave}
|
||||
okText="保存"
|
||||
confirmLoading={phoneSaving}
|
||||
width={420}
|
||||
>
|
||||
<Form form={phoneForm} layout="vertical">
|
||||
<Form.Item
|
||||
name="phone"
|
||||
label="手机号"
|
||||
rules={[
|
||||
{ required: true, message: '请输入手机号' },
|
||||
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的 11 位手机号' },
|
||||
]}
|
||||
>
|
||||
<Input placeholder="11 位手机号" maxLength={11} autoComplete="tel" />
|
||||
</Form.Item>
|
||||
<Alert
|
||||
type="info"
|
||||
showIcon
|
||||
message="绑定后该账号必须使用「密码 + 短信验证码 + 本机 DCAuth 设备」登录。"
|
||||
/>
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user