diff --git a/packages/plugins/@nocobase/plugin-auth/src/server/actions/auth.ts b/packages/plugins/@nocobase/plugin-auth/src/server/actions/auth.ts index 81ca2c90e2..71f18907db 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/server/actions/auth.ts +++ b/packages/plugins/@nocobase/plugin-auth/src/server/actions/auth.ts @@ -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('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(); }, }; diff --git a/packages/plugins/@nocobase/plugin-auth/src/server/basic-auth.ts b/packages/plugins/@nocobase/plugin-auth/src/server/basic-auth.ts index c77b76ba0a..13c80bada1 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/server/basic-auth.ts +++ b/packages/plugins/@nocobase/plugin-auth/src/server/basic-auth.ts @@ -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('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; - } } diff --git a/packages/plugins/@nocobase/plugin-auth/src/swagger/index.ts b/packages/plugins/@nocobase/plugin-auth/src/swagger/index.ts index 4b066083b1..52ffbf7ee2 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/swagger/index.ts +++ b/packages/plugins/@nocobase/plugin-auth/src/swagger/index.ts @@ -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',