From a64f52ecbbebb52aed9389f8cf2029a11742fbb6 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 29 Apr 2024 23:04:59 +0800 Subject: [PATCH] update user admin --- app/api/admin/users/[[...path]]/route.ts | 23 +++-- app/api/test/route.ts | 0 app/app/(admin)/components/users-table.tsx | 103 ++++++++++++++++++++- lib/auth.ts | 8 +- 4 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 app/api/test/route.ts diff --git a/app/api/admin/users/[[...path]]/route.ts b/app/api/admin/users/[[...path]]/route.ts index 95e989ac8..ebe9cc7e6 100644 --- a/app/api/admin/users/[[...path]]/route.ts +++ b/app/api/admin/users/[[...path]]/route.ts @@ -95,7 +95,16 @@ async function handle( if (method === "PUT") { try { const userId = params.path[0]; - return await changeUserInfo(userId, await req.json()); + let new_user_info: Partial = Object.entries( + await req.json(), + ).reduce((acc, [key, value]) => { + if (value !== null) { + // @ts-ignore + acc[key] = value; + } + return acc; + }, {}); + return await changeUserInfo(userId, new_user_info); } catch { return NextResponse.json({ error: "未知错误" }, { status: 500 }); } @@ -103,16 +112,18 @@ async function handle( return NextResponse.json({ error: "当前方法不支持" }, { status: 405 }); } -async function changeUserInfo(id: string, info: User) { - const hashDPassword = hashPassword(info?.password ?? ""); - console.log("-----------", id, info, hashDPassword); - if (hashDPassword) { +async function changeUserInfo(id: string, info: Partial) { + if (info.password) { + info["password"] = hashPassword(info.password); + } + // console.log("-----------", id, info, hashDPassword); + if (info) { await prisma.user.update({ where: { id: id, }, data: { - password: hashDPassword, + ...info, }, }); return NextResponse.json({ result: "ok" }); diff --git a/app/api/test/route.ts b/app/api/test/route.ts new file mode 100644 index 000000000..e69de29bb diff --git a/app/app/(admin)/components/users-table.tsx b/app/app/(admin)/components/users-table.tsx index adbed2185..6b13820fc 100644 --- a/app/app/(admin)/components/users-table.tsx +++ b/app/app/(admin)/components/users-table.tsx @@ -15,6 +15,7 @@ import { Form, } from "antd"; import type { GetRef, TableColumnsType } from "antd"; +import { LockOutlined } from "@ant-design/icons"; // import { headers } from 'next/headers' import type { NotificationArgsProps } from "antd"; @@ -97,10 +98,100 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { setNewPassword(e.target.value.trim()); }; - const handleUserEdit = ( - method: "POST" | "PUT", - record: User | undefined, - ) => {}; + const handleUserEdit = (method: "POST" | "PUT", record: User | undefined) => { + editUserModal.confirm({ + title: "编辑用户", + content: ( +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + } + type="password" + placeholder="Password" + autoComplete="new-password" + /> + +
+ ), + onOk: () => { + const setting_key = method === "PUT" ? record?.id : ""; + editUserForm.validateFields().then((values) => { + const dataToSubmit = { + username: values.username ?? null, + email: values.email ?? null, + allowToLogin: values.allowToLogin ?? true, + isAdmin: values.isAdmin ?? false, + password: values.password ?? null, + }; + fetch(`/api/admin/users/${values.id}`, { + method: method, + credentials: "include", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(dataToSubmit), + }) + .then((response) => response.json()) + .then((result) => { + if (result["result"] == "ok") { + openNotification("info", { + message: "修改信息", + description: `${values.id} 信息修改成功`, + }); + } + }) + .catch((error) => { + console.log("e", error); + openNotification("error", { + message: "修改信息", + description: `${values.id} 信息修改失败`, + }); + }); + }); + }, + }); + }; const confirmPassword = async (id: string) => { console.log("-----", newPassword, id); @@ -240,7 +331,9 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { key: "id", render: (_, record) => ( - 编辑 + handleUserEdit("PUT", record)}> + 编辑 + ; + export const authOptions: NextAuthOptions = { @@ -89,7 +89,7 @@ export const authOptions: NextAuthOptions = { // 判断姓名格式是否符合要求,不符合则拒绝 if (username && isName(username)) { // Any object returned will be saved in `user` property of the JWT - let user: PartialUser = {} + let user: Partial = {} if (isEmail(username)) { user['email'] = username; } else { @@ -228,7 +228,7 @@ async function getSetting(key: string) { } } -async function existUser(user: PartialUser ) { +async function existUser(user: Partial ) { const conditions = []; if (user?.name) { conditions.push({ name: user.name }); @@ -243,7 +243,7 @@ async function existUser(user: PartialUser ) { }) : null } -export async function insertUser(user: PartialUser ) { +export async function insertUser(user: Partial ) { console.log('------------', user) try { return await prisma.user.create({