Merge branch 'main' into next

This commit is contained in:
nocobase[bot] 2024-11-07 06:52:34 +00:00
commit a671fff804
3 changed files with 202 additions and 205 deletions

View File

@ -9,22 +9,52 @@
/* istanbul ignore file -- @preserve */
import { Context, Next } from '@nocobase/actions';
import { PasswordField } from '@nocobase/database';
import { namespace } from '../../preset';
export default {
lostPassword: async (ctx: Context, next: Next) => {
ctx.body = await ctx.auth.lostPassword();
await next();
},
resetPassword: async (ctx: Context, next: Next) => {
ctx.body = await ctx.auth.resetPassword();
await next();
},
getUserByResetToken: async (ctx: Context, next: Next) => {
ctx.body = await ctx.auth.getUserByResetToken();
await next();
},
// lostPassword: async (ctx: Context, next: Next) => {
// ctx.body = await ctx.auth.lostPassword();
// await next();
// },
// resetPassword: async (ctx: Context, next: Next) => {
// ctx.body = await ctx.auth.resetPassword();
// await next();
// },
// getUserByResetToken: async (ctx: Context, next: Next) => {
// ctx.body = await ctx.auth.getUserByResetToken();
// await next();
// },
changePassword: async (ctx: Context, next: Next) => {
ctx.body = await ctx.auth.changePassword();
const {
values: { oldPassword, newPassword, confirmPassword },
} = ctx.action.params;
if (newPassword !== confirmPassword) {
ctx.throw(400, ctx.t('The password is inconsistent, please re-enter', { ns: namespace }));
}
const currentUser = ctx.auth.user;
if (!currentUser) {
ctx.throw(401);
}
let key: string;
if (currentUser.username) {
key = 'username';
} else {
key = 'email';
}
const user = await ctx.db.getRepository('users').findOne({
where: {
[key]: currentUser[key],
},
});
const pwd = ctx.db.getCollection('users').getField<PasswordField>('password');
const isValid = await pwd.verify(oldPassword, user.password);
if (!isValid) {
ctx.throw(401, ctx.t('The password is incorrect, please re-enter', { ns: namespace }));
}
user.password = newPassword;
await user.save();
ctx.body = currentUser;
await next();
},
};

View File

@ -130,37 +130,4 @@ export class BasicAuth extends BaseAuth {
}
return user;
}
async changePassword() {
const ctx = this.ctx;
const {
values: { oldPassword, newPassword, confirmPassword },
} = ctx.action.params;
if (newPassword !== confirmPassword) {
ctx.throw(400, ctx.t('The password is inconsistent, please re-enter', { ns: namespace }));
}
const currentUser = ctx.auth.user;
if (!currentUser) {
ctx.throw(401);
}
let key: string;
if (currentUser.username) {
key = 'username';
} else {
key = 'email';
}
const user = await this.userRepository.findOne({
where: {
[key]: currentUser[key],
},
});
const pwd = this.userCollection.getField<PasswordField>('password');
const isValid = await pwd.verify(oldPassword, user.password);
if (!isValid) {
ctx.throw(401, ctx.t('The password is incorrect, please re-enter', { ns: namespace }));
}
user.password = newPassword;
await user.save();
return currentUser;
}
}

View File

@ -168,165 +168,165 @@ export default {
},
},
},
'/auth:lostPassword': {
post: {
description: 'Lost password',
tags: ['Basic auth'],
security: [],
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
email: {
type: 'string',
description: '邮箱',
},
},
},
},
},
},
responses: {
200: {
description: 'successful operation',
content: {
'application/json': {
schema: {
allOf: [
{
$ref: '#/components/schemas/user',
},
{
type: 'object',
properties: {
resetToken: {
type: 'string',
description: '重置密码的token',
},
},
},
],
},
},
},
},
400: {
description: 'Please fill in your email address',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/error',
},
},
},
},
401: {
description: 'The email is incorrect, please re-enter',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/error',
},
},
},
},
},
},
},
'/auth:resetPassword': {
post: {
description: 'Reset password',
tags: ['Basic auth'],
security: [],
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
email: {
type: 'string',
description: '邮箱',
},
password: {
type: 'string',
description: '密码',
},
resetToken: {
type: 'string',
description: '重置密码的token',
},
},
},
},
},
},
responses: {
200: {
description: 'successful operation',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/user',
},
},
},
},
404: {
description: 'User not found',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/error',
},
},
},
},
},
},
},
'/auth:getUserByResetToken': {
get: {
description: 'Get user by reset token',
tags: ['Basic auth'],
security: [],
parameters: [
{
name: 'token',
in: 'query',
description: '重置密码的token',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'ok',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/user',
},
},
},
},
401: {
description: 'Unauthorized',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/error',
},
},
},
},
},
},
},
// '/auth:lostPassword': {
// post: {
// description: 'Lost password',
// tags: ['Basic auth'],
// security: [],
// requestBody: {
// content: {
// 'application/json': {
// schema: {
// type: 'object',
// properties: {
// email: {
// type: 'string',
// description: '邮箱',
// },
// },
// },
// },
// },
// },
// responses: {
// 200: {
// description: 'successful operation',
// content: {
// 'application/json': {
// schema: {
// allOf: [
// {
// $ref: '#/components/schemas/user',
// },
// {
// type: 'object',
// properties: {
// resetToken: {
// type: 'string',
// description: '重置密码的token',
// },
// },
// },
// ],
// },
// },
// },
// },
// 400: {
// description: 'Please fill in your email address',
// content: {
// 'application/json': {
// schema: {
// $ref: '#/components/schemas/error',
// },
// },
// },
// },
// 401: {
// description: 'The email is incorrect, please re-enter',
// content: {
// 'application/json': {
// schema: {
// $ref: '#/components/schemas/error',
// },
// },
// },
// },
// },
// },
// },
// '/auth:resetPassword': {
// post: {
// description: 'Reset password',
// tags: ['Basic auth'],
// security: [],
// requestBody: {
// content: {
// 'application/json': {
// schema: {
// type: 'object',
// properties: {
// email: {
// type: 'string',
// description: '邮箱',
// },
// password: {
// type: 'string',
// description: '密码',
// },
// resetToken: {
// type: 'string',
// description: '重置密码的token',
// },
// },
// },
// },
// },
// },
// responses: {
// 200: {
// description: 'successful operation',
// content: {
// 'application/json': {
// schema: {
// $ref: '#/components/schemas/user',
// },
// },
// },
// },
// 404: {
// description: 'User not found',
// content: {
// 'application/json': {
// schema: {
// $ref: '#/components/schemas/error',
// },
// },
// },
// },
// },
// },
// },
// '/auth:getUserByResetToken': {
// get: {
// description: 'Get user by reset token',
// tags: ['Basic auth'],
// security: [],
// parameters: [
// {
// name: 'token',
// in: 'query',
// description: '重置密码的token',
// required: true,
// schema: {
// type: 'string',
// },
// },
// ],
// responses: {
// 200: {
// description: 'ok',
// content: {
// 'application/json': {
// schema: {
// $ref: '#/components/schemas/user',
// },
// },
// },
// },
// 401: {
// description: 'Unauthorized',
// content: {
// 'application/json': {
// schema: {
// $ref: '#/components/schemas/error',
// },
// },
// },
// },
// },
// },
// },
'/auth:changePassword': {
post: {
description: 'Change password',