From 64283fcb0eff479eb0c2062798455a9539ff86d2 Mon Sep 17 00:00:00 2001 From: SemmyWong <67748948+semmywong@users.noreply.github.com> Date: Thu, 25 Nov 2021 22:36:52 +0800 Subject: [PATCH] feat: allow user to change password (#109) * feat: allow user to change password * docs: update api doc Co-authored-by: chenos --- .../src/components/admin-layout/UserInfo.tsx | 54 ++++++++++++++++++- packages/client/src/locale/en_US.ts | 3 ++ packages/client/src/locale/zh_CN.ts | 3 ++ packages/plugin-users/src/actions/users.ts | 23 ++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/packages/client/src/components/admin-layout/UserInfo.tsx b/packages/client/src/components/admin-layout/UserInfo.tsx index 21e6f6ada..c90614b01 100644 --- a/packages/client/src/components/admin-layout/UserInfo.tsx +++ b/packages/client/src/components/admin-layout/UserInfo.tsx @@ -1,5 +1,5 @@ import React, { useContext, useEffect } from 'react'; -import { Button, Dropdown, Menu, Select } from 'antd'; +import { Button, Dropdown, Menu, Select, message } from 'antd'; import { useHistory } from 'react-router-dom'; import { SchemaField } from '../../schemas'; import { AuthContext, useCurrentUser } from './Auth'; @@ -94,6 +94,58 @@ export const UserInfo = () => { /> + { + setVisible(false); + const values = await FormDrawer(t('Change password'), () => { + return ( + + + + + { + const { data } = await request( + 'users:changePassword', + { + method: 'post', + data: values, + }, + ).catch((e) => { + message.error(e.data); + throw e; + }); + }} + > + {t('Submit')} + + + + + ); + }).open(); + }} + > + {t('Change password')} +
diff --git a/packages/client/src/locale/en_US.ts b/packages/client/src/locale/en_US.ts index 1ef5a4783..9ca37ca3f 100644 --- a/packages/client/src/locale/en_US.ts +++ b/packages/client/src/locale/en_US.ts @@ -33,6 +33,9 @@ export default { 'Collections & Fields': 'Collections & Fields', 'Roles & Permissions': 'Roles & Permissions', 'Edit profile': 'Edit profile', + 'Change password': 'Change password', + 'Old password': 'Old password', + 'New password': 'New password', 'Switch role': 'Switch role', 'Super admin': 'Super admin', 'Language': 'Language', diff --git a/packages/client/src/locale/zh_CN.ts b/packages/client/src/locale/zh_CN.ts index c8fbfa910..ea0432662 100644 --- a/packages/client/src/locale/zh_CN.ts +++ b/packages/client/src/locale/zh_CN.ts @@ -34,6 +34,9 @@ export default { 'Collections & Fields': '数据表配置', 'Roles & Permissions': '角色和权限', 'Edit profile': '个人资料', + 'Change password': '修改密码', + 'Old password': '旧密码', + 'New password': '新密码', 'Switch role': '切换角色', 'Super admin': '超级管理员', 'Language': '语言设置', diff --git a/packages/plugin-users/src/actions/users.ts b/packages/plugin-users/src/actions/users.ts index e56db3d2f..58e0f702a 100644 --- a/packages/plugin-users/src/actions/users.ts +++ b/packages/plugin-users/src/actions/users.ts @@ -129,3 +129,26 @@ export async function updateProfile(ctx: Context, next: Next) { ctx.body = ctx.state.currentUser; await next(); } + +export async function changePassword(ctx: Context, next: Next) { + const { + values: { oldPassword, newPassword }, + } = ctx.action.params; + if (!ctx.state.currentUser) { + ctx.throw(401, 'Unauthorized'); + } + const User = ctx.db.getModel('users'); + const user = await User.scope('withPassword').findOne({ + where: { + email: ctx.state.currentUser.email, + }, + }); + const isValid = await PASSWORD.verify(oldPassword, user.password); + if (!isValid) { + ctx.throw(401, '密码错误,请您重新输入'); + } + user.password = newPassword; + user.save(); + ctx.body = ctx.state.currentUser.toJSON(); + await next(); +}