feat: allow user to change password (#109)

* feat: allow user to change password

* docs: update api doc

Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
SemmyWong 2021-11-25 22:36:52 +08:00 committed by GitHub
parent 1daa8138c1
commit 64283fcb0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 1 deletions

View File

@ -1,5 +1,5 @@
import React, { useContext, useEffect } from 'react'; 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 { useHistory } from 'react-router-dom';
import { SchemaField } from '../../schemas'; import { SchemaField } from '../../schemas';
import { AuthContext, useCurrentUser } from './Auth'; import { AuthContext, useCurrentUser } from './Auth';
@ -94,6 +94,58 @@ export const UserInfo = () => {
/> />
</div> </div>
</Menu.Item> </Menu.Item>
<Menu.Item
onClick={async () => {
setVisible(false);
const values = await FormDrawer(t('Change password'), () => {
return (
<FormLayout layout={'vertical'}>
<SchemaField
schema={{
type: 'object',
properties: {
oldPassword: {
type: 'string',
title: t('Old password'),
'x-component': 'Password',
'x-decorator': 'FormilyFormItem',
},
newPassword: {
type: 'string',
title: t('New password'),
'x-component': 'Password',
'x-decorator': 'FormilyFormItem',
},
},
}}
/>
<FormDrawer.Footer>
<FormButtonGroup align="right">
<Submit
onSubmit={async (values) => {
const { data } = await request(
'users:changePassword',
{
method: 'post',
data: values,
},
).catch((e) => {
message.error(e.data);
throw e;
});
}}
>
{t('Submit')}
</Submit>
</FormButtonGroup>
</FormDrawer.Footer>
</FormLayout>
);
}).open();
}}
>
{t('Change password')}
</Menu.Item>
<Menu.Item> <Menu.Item>
<div style={{ display: 'flex', justifyContent: 'space-between' }}> <div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ marginRight: 16, display: 'inline-block' }}> <span style={{ marginRight: 16, display: 'inline-block' }}>

View File

@ -33,6 +33,9 @@ export default {
'Collections & Fields': 'Collections & Fields', 'Collections & Fields': 'Collections & Fields',
'Roles & Permissions': 'Roles & Permissions', 'Roles & Permissions': 'Roles & Permissions',
'Edit profile': 'Edit profile', 'Edit profile': 'Edit profile',
'Change password': 'Change password',
'Old password': 'Old password',
'New password': 'New password',
'Switch role': 'Switch role', 'Switch role': 'Switch role',
'Super admin': 'Super admin', 'Super admin': 'Super admin',
'Language': 'Language', 'Language': 'Language',

View File

@ -34,6 +34,9 @@ export default {
'Collections & Fields': '数据表配置', 'Collections & Fields': '数据表配置',
'Roles & Permissions': '角色和权限', 'Roles & Permissions': '角色和权限',
'Edit profile': '个人资料', 'Edit profile': '个人资料',
'Change password': '修改密码',
'Old password': '旧密码',
'New password': '新密码',
'Switch role': '切换角色', 'Switch role': '切换角色',
'Super admin': '超级管理员', 'Super admin': '超级管理员',
'Language': '语言设置', 'Language': '语言设置',

View File

@ -129,3 +129,26 @@ export async function updateProfile(ctx: Context, next: Next) {
ctx.body = ctx.state.currentUser; ctx.body = ctx.state.currentUser;
await next(); 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();
}