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:
parent
1daa8138c1
commit
64283fcb0e
@ -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 = () => {
|
||||
/>
|
||||
</div>
|
||||
</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>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
|
||||
<span style={{ marginRight: 16, display: 'inline-block' }}>
|
||||
|
@ -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',
|
||||
|
@ -34,6 +34,9 @@ export default {
|
||||
'Collections & Fields': '数据表配置',
|
||||
'Roles & Permissions': '角色和权限',
|
||||
'Edit profile': '个人资料',
|
||||
'Change password': '修改密码',
|
||||
'Old password': '旧密码',
|
||||
'New password': '新密码',
|
||||
'Switch role': '切换角色',
|
||||
'Super admin': '超级管理员',
|
||||
'Language': '语言设置',
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user