@ruiapp/rapid-core 0.1.27 → 0.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ var qs = require('qs');
9
9
  var jsonwebtoken = require('jsonwebtoken');
10
10
  var crypto = require('crypto');
11
11
  var dayjs = require('dayjs');
12
+ var bcrypt = require('bcrypt');
12
13
  var path = require('path');
13
14
  var fs = require('fs');
14
15
  var uuid = require('uuid');
@@ -19,6 +20,7 @@ var Router__default = /*#__PURE__*/_interopDefaultLegacy(Router);
19
20
  var qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
20
21
  var crypto__default = /*#__PURE__*/_interopDefaultLegacy(crypto);
21
22
  var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
23
+ var bcrypt__default = /*#__PURE__*/_interopDefaultLegacy(bcrypt);
22
24
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
23
25
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
24
26
 
@@ -2613,8 +2615,18 @@ class RapidServer {
2613
2615
  const rapidRequest = new RapidRequest(this, request);
2614
2616
  await rapidRequest.parseBody();
2615
2617
  const routeContext = new RouteContext(this, rapidRequest);
2616
- await this.#pluginManager.onPrepareRouteContext(routeContext);
2617
- await this.#buildedRoutes(routeContext, next);
2618
+ try {
2619
+ await this.#pluginManager.onPrepareRouteContext(routeContext);
2620
+ await this.#buildedRoutes(routeContext, next);
2621
+ }
2622
+ catch (ex) {
2623
+ this.#logger.error('handle request error:', ex);
2624
+ routeContext.response.json({
2625
+ error: {
2626
+ message: ex.message || ex,
2627
+ },
2628
+ }, 500);
2629
+ }
2618
2630
  return routeContext.response.getResponse();
2619
2631
  }
2620
2632
  async beforeRunRouteActions(handlerContext) {
@@ -2772,32 +2784,32 @@ async function generateJwtSecretKey() {
2772
2784
  return encode(exportedKey);
2773
2785
  }
2774
2786
 
2775
- const code$l = "listMetaModels";
2776
- async function handler$l(plugin, ctx, options) {
2787
+ const code$m = "listMetaModels";
2788
+ async function handler$m(plugin, ctx, options) {
2777
2789
  const { applicationConfig } = ctx;
2778
2790
  ctx.output = { list: applicationConfig.models };
2779
2791
  }
2780
2792
 
2781
2793
  var listMetaModels = /*#__PURE__*/Object.freeze({
2782
2794
  __proto__: null,
2783
- code: code$l,
2784
- handler: handler$l
2795
+ code: code$m,
2796
+ handler: handler$m
2785
2797
  });
2786
2798
 
2787
- const code$k = "listMetaRoutes";
2788
- async function handler$k(plugin, ctx, options) {
2799
+ const code$l = "listMetaRoutes";
2800
+ async function handler$l(plugin, ctx, options) {
2789
2801
  const { applicationConfig } = ctx;
2790
2802
  ctx.output = { list: applicationConfig.routes };
2791
2803
  }
2792
2804
 
2793
2805
  var listMetaRoutes = /*#__PURE__*/Object.freeze({
2794
2806
  __proto__: null,
2795
- code: code$k,
2796
- handler: handler$k
2807
+ code: code$l,
2808
+ handler: handler$l
2797
2809
  });
2798
2810
 
2799
- const code$j = "getMetaModelDetail";
2800
- async function handler$j(plugin, ctx, options) {
2811
+ const code$k = "getMetaModelDetail";
2812
+ async function handler$k(plugin, ctx, options) {
2801
2813
  const { server, input } = ctx;
2802
2814
  const model = server.getModel(input);
2803
2815
  ctx.output = model;
@@ -2805,8 +2817,8 @@ async function handler$j(plugin, ctx, options) {
2805
2817
 
2806
2818
  var getMetaModelDetail = /*#__PURE__*/Object.freeze({
2807
2819
  __proto__: null,
2808
- code: code$j,
2809
- handler: handler$j
2820
+ code: code$k,
2821
+ handler: handler$k
2810
2822
  });
2811
2823
 
2812
2824
  /**
@@ -3158,9 +3170,9 @@ function transformFilterWithSubFilters(filter) {
3158
3170
  return filter;
3159
3171
  }
3160
3172
 
3161
- const code$i = "findCollectionEntities";
3162
- async function handler$i(plugin, ctx, options) {
3163
- await runCollectionEntityActionHandler(ctx, options, code$i, async (entityManager, input) => {
3173
+ const code$j = "findCollectionEntities";
3174
+ async function handler$j(plugin, ctx, options) {
3175
+ await runCollectionEntityActionHandler(ctx, options, code$j, async (entityManager, input) => {
3164
3176
  input.filters = removeFiltersWithNullValue(input.filters);
3165
3177
  const entities = await entityManager.findEntities(input);
3166
3178
  const result = { list: entities };
@@ -3175,14 +3187,14 @@ async function handler$i(plugin, ctx, options) {
3175
3187
 
3176
3188
  var findCollectionEntities = /*#__PURE__*/Object.freeze({
3177
3189
  __proto__: null,
3178
- code: code$i,
3179
- handler: handler$i
3190
+ code: code$j,
3191
+ handler: handler$j
3180
3192
  });
3181
3193
 
3182
- const code$h = "findCollectionEntityById";
3183
- async function handler$h(plugin, ctx, options) {
3194
+ const code$i = "findCollectionEntityById";
3195
+ async function handler$i(plugin, ctx, options) {
3184
3196
  const { logger, server, input } = ctx;
3185
- logger.debug(`Running ${code$h} handler...`, { input });
3197
+ logger.debug(`Running ${code$i} handler...`, { input });
3186
3198
  const { id } = input;
3187
3199
  const entityManager = server.getEntityManager(options.singularCode);
3188
3200
  const entity = await entityManager.findById(id);
@@ -3194,13 +3206,13 @@ async function handler$h(plugin, ctx, options) {
3194
3206
 
3195
3207
  var findCollectionEntityById = /*#__PURE__*/Object.freeze({
3196
3208
  __proto__: null,
3197
- code: code$h,
3198
- handler: handler$h
3209
+ code: code$i,
3210
+ handler: handler$i
3199
3211
  });
3200
3212
 
3201
- const code$g = "countCollectionEntities";
3202
- async function handler$g(plugin, ctx, options) {
3203
- await runCollectionEntityActionHandler(ctx, options, code$g, (entityManager, input) => {
3213
+ const code$h = "countCollectionEntities";
3214
+ async function handler$h(plugin, ctx, options) {
3215
+ await runCollectionEntityActionHandler(ctx, options, code$h, (entityManager, input) => {
3204
3216
  input.filters = removeFiltersWithNullValue(input.filters);
3205
3217
  return entityManager.count(input);
3206
3218
  });
@@ -3208,16 +3220,16 @@ async function handler$g(plugin, ctx, options) {
3208
3220
 
3209
3221
  var countCollectionEntities = /*#__PURE__*/Object.freeze({
3210
3222
  __proto__: null,
3211
- code: code$g,
3212
- handler: handler$g
3223
+ code: code$h,
3224
+ handler: handler$h
3213
3225
  });
3214
3226
 
3215
- const code$f = "createCollectionEntity";
3216
- async function handler$f(plugin, ctx, options) {
3227
+ const code$g = "createCollectionEntity";
3228
+ async function handler$g(plugin, ctx, options) {
3217
3229
  const { logger, server, input } = ctx;
3218
3230
  const { defaultInput, fixedInput } = options;
3219
3231
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3220
- logger.debug(`Running ${code$f} handler...`, { defaultInput, fixedInput, mergedInput });
3232
+ logger.debug(`Running ${code$g} handler...`, { defaultInput, fixedInput, mergedInput });
3221
3233
  const userId = ctx.routerContext.state?.userId;
3222
3234
  if (userId) {
3223
3235
  input.createdBy = userId;
@@ -3231,15 +3243,15 @@ async function handler$f(plugin, ctx, options) {
3231
3243
 
3232
3244
  var createCollectionEntity = /*#__PURE__*/Object.freeze({
3233
3245
  __proto__: null,
3234
- code: code$f,
3235
- handler: handler$f
3246
+ code: code$g,
3247
+ handler: handler$g
3236
3248
  });
3237
3249
 
3238
- const code$e = "createCollectionEntitiesBatch";
3239
- async function handler$e(plugin, ctx, options) {
3250
+ const code$f = "createCollectionEntitiesBatch";
3251
+ async function handler$f(plugin, ctx, options) {
3240
3252
  const { logger, server, input } = ctx;
3241
3253
  const { defaultInput, fixedInput } = options;
3242
- logger.debug(`Running ${code$e} handler...`, { defaultInput, fixedInput, input });
3254
+ logger.debug(`Running ${code$f} handler...`, { defaultInput, fixedInput, input });
3243
3255
  const { entities } = input;
3244
3256
  if (!lodash.isArray(entities)) {
3245
3257
  throw new Error("input.entities should be an array.");
@@ -3262,16 +3274,16 @@ async function handler$e(plugin, ctx, options) {
3262
3274
 
3263
3275
  var createCollectionEntitiesBatch = /*#__PURE__*/Object.freeze({
3264
3276
  __proto__: null,
3265
- code: code$e,
3266
- handler: handler$e
3277
+ code: code$f,
3278
+ handler: handler$f
3267
3279
  });
3268
3280
 
3269
- const code$d = "updateCollectionEntityById";
3270
- async function handler$d(plugin, ctx, options) {
3281
+ const code$e = "updateCollectionEntityById";
3282
+ async function handler$e(plugin, ctx, options) {
3271
3283
  const { logger, server, input } = ctx;
3272
3284
  const { defaultInput, fixedInput } = options;
3273
3285
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3274
- logger.debug(`Running ${code$d} handler...`, { defaultInput, fixedInput, mergedInput });
3286
+ logger.debug(`Running ${code$e} handler...`, { defaultInput, fixedInput, mergedInput });
3275
3287
  const entityManager = server.getEntityManager(options.singularCode);
3276
3288
  const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput }, plugin);
3277
3289
  ctx.output = output;
@@ -3279,14 +3291,14 @@ async function handler$d(plugin, ctx, options) {
3279
3291
 
3280
3292
  var updateCollectionEntityById = /*#__PURE__*/Object.freeze({
3281
3293
  __proto__: null,
3282
- code: code$d,
3283
- handler: handler$d
3294
+ code: code$e,
3295
+ handler: handler$e
3284
3296
  });
3285
3297
 
3286
- const code$c = "deleteCollectionEntityById";
3287
- async function handler$c(plugin, ctx, options) {
3298
+ const code$d = "deleteCollectionEntityById";
3299
+ async function handler$d(plugin, ctx, options) {
3288
3300
  const { logger, server, input } = ctx;
3289
- logger.debug(`Running ${code$c} handler...`);
3301
+ logger.debug(`Running ${code$d} handler...`);
3290
3302
  const entityManager = server.getEntityManager(options.singularCode);
3291
3303
  await entityManager.deleteById(input.id, plugin);
3292
3304
  ctx.status = 200;
@@ -3295,16 +3307,16 @@ async function handler$c(plugin, ctx, options) {
3295
3307
 
3296
3308
  var deleteCollectionEntityById = /*#__PURE__*/Object.freeze({
3297
3309
  __proto__: null,
3298
- code: code$c,
3299
- handler: handler$c
3310
+ code: code$d,
3311
+ handler: handler$d
3300
3312
  });
3301
3313
 
3302
- const code$b = "addEntityRelations";
3303
- async function handler$b(plugin, ctx, options) {
3314
+ const code$c = "addEntityRelations";
3315
+ async function handler$c(plugin, ctx, options) {
3304
3316
  const { logger, server, input } = ctx;
3305
3317
  const { defaultInput, fixedInput } = options;
3306
3318
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3307
- logger.debug(`Running ${code$b} handler...`, { defaultInput, fixedInput, mergedInput });
3319
+ logger.debug(`Running ${code$c} handler...`, { defaultInput, fixedInput, mergedInput });
3308
3320
  const entityManager = server.getEntityManager(options.singularCode);
3309
3321
  await entityManager.addRelations(mergedInput, plugin);
3310
3322
  ctx.output = {};
@@ -3312,16 +3324,16 @@ async function handler$b(plugin, ctx, options) {
3312
3324
 
3313
3325
  var addEntityRelations = /*#__PURE__*/Object.freeze({
3314
3326
  __proto__: null,
3315
- code: code$b,
3316
- handler: handler$b
3327
+ code: code$c,
3328
+ handler: handler$c
3317
3329
  });
3318
3330
 
3319
- const code$a = "removeEntityRelations";
3320
- async function handler$a(plugin, ctx, options) {
3331
+ const code$b = "removeEntityRelations";
3332
+ async function handler$b(plugin, ctx, options) {
3321
3333
  const { logger, server, input } = ctx;
3322
3334
  const { defaultInput, fixedInput } = options;
3323
3335
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3324
- logger.debug(`Running ${code$a} handler...`, { defaultInput, fixedInput, mergedInput });
3336
+ logger.debug(`Running ${code$b} handler...`, { defaultInput, fixedInput, mergedInput });
3325
3337
  const entityManager = server.getEntityManager(options.singularCode);
3326
3338
  await entityManager.removeRelations(mergedInput, plugin);
3327
3339
  ctx.output = {};
@@ -3329,16 +3341,16 @@ async function handler$a(plugin, ctx, options) {
3329
3341
 
3330
3342
  var removeEntityRelations = /*#__PURE__*/Object.freeze({
3331
3343
  __proto__: null,
3332
- code: code$a,
3333
- handler: handler$a
3344
+ code: code$b,
3345
+ handler: handler$b
3334
3346
  });
3335
3347
 
3336
- const code$9 = "queryDatabase";
3337
- async function handler$9(plugin, ctx, options) {
3348
+ const code$a = "queryDatabase";
3349
+ async function handler$a(plugin, ctx, options) {
3338
3350
  const { logger, server, input } = ctx;
3339
3351
  const { sql, querySingle, defaultInput, fixedInput } = options;
3340
3352
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3341
- logger.debug(`Running ${code$9} handler...`, { defaultInput, fixedInput, mergedInput });
3353
+ logger.debug(`Running ${code$a} handler...`, { defaultInput, fixedInput, mergedInput });
3342
3354
  const result = await server.queryDatabaseObject(sql, mergedInput);
3343
3355
  if (querySingle) {
3344
3356
  ctx.output = lodash.first(result);
@@ -3350,8 +3362,8 @@ async function handler$9(plugin, ctx, options) {
3350
3362
 
3351
3363
  var queryDatabase = /*#__PURE__*/Object.freeze({
3352
3364
  __proto__: null,
3353
- code: code$9,
3354
- handler: handler$9
3365
+ code: code$a,
3366
+ handler: handler$a
3355
3367
  });
3356
3368
 
3357
3369
  /**
@@ -3522,17 +3534,17 @@ async function sendSourceResponse(proxyCtx, targetRes) {
3522
3534
  srcRes.body = targetRes.body;
3523
3535
  }
3524
3536
 
3525
- const code$8 = "httpProxy";
3526
- async function handler$8(plugin, ctx, options) {
3537
+ const code$9 = "httpProxy";
3538
+ async function handler$9(plugin, ctx, options) {
3527
3539
  const { logger } = ctx;
3528
- logger.debug(`Running ${code$8} handler...`);
3540
+ logger.debug(`Running ${code$9} handler...`);
3529
3541
  await doProxy(ctx.routerContext, options);
3530
3542
  }
3531
3543
 
3532
3544
  var httpProxy = /*#__PURE__*/Object.freeze({
3533
3545
  __proto__: null,
3534
- code: code$8,
3535
- handler: handler$8
3546
+ code: code$9,
3547
+ handler: handler$9
3536
3548
  });
3537
3549
 
3538
3550
  /**
@@ -3771,8 +3783,8 @@ async function generateSn$2(server, input) {
3771
3783
  return sequenceNumbers;
3772
3784
  }
3773
3785
 
3774
- const code$7 = "generateSn";
3775
- async function handler$7(plugin, ctx, options) {
3786
+ const code$8 = "generateSn";
3787
+ async function handler$8(plugin, ctx, options) {
3776
3788
  const { server, routerContext } = ctx;
3777
3789
  const input = ctx.input;
3778
3790
  if (options?.ruleCode) {
@@ -3789,8 +3801,8 @@ async function handler$7(plugin, ctx, options) {
3789
3801
 
3790
3802
  var generateSn$1 = /*#__PURE__*/Object.freeze({
3791
3803
  __proto__: null,
3792
- code: code$7,
3793
- handler: handler$7
3804
+ code: code$8,
3805
+ handler: handler$8
3794
3806
  });
3795
3807
 
3796
3808
  var pluginActionHandlers$2 = [
@@ -4180,8 +4192,8 @@ class WebhooksPlugin {
4180
4192
  }
4181
4193
  }
4182
4194
 
4183
- const code$6 = "createSession";
4184
- async function handler$6(plugin, ctx, options) {
4195
+ const code$7 = "createSession";
4196
+ async function handler$7(plugin, ctx, options) {
4185
4197
  const { server, input, routerContext } = ctx;
4186
4198
  const { response } = routerContext;
4187
4199
  const { account, password } = input;
@@ -4198,7 +4210,11 @@ async function handler$6(plugin, ctx, options) {
4198
4210
  ]
4199
4211
  });
4200
4212
  if (!user) {
4201
- throw new Error("Wrong account or password.");
4213
+ throw new Error("用户名或密码错误。");
4214
+ }
4215
+ const isMatch = await bcrypt__default["default"].compare(password, user.password);
4216
+ if (!isMatch) {
4217
+ throw new Error("用户名或密码错误。");
4202
4218
  }
4203
4219
  const secretKey = Buffer.from(server.config.jwtKey, "base64");
4204
4220
  const token = createJwt({
@@ -4220,12 +4236,12 @@ async function handler$6(plugin, ctx, options) {
4220
4236
 
4221
4237
  var createSession = /*#__PURE__*/Object.freeze({
4222
4238
  __proto__: null,
4223
- code: code$6,
4224
- handler: handler$6
4239
+ code: code$7,
4240
+ handler: handler$7
4225
4241
  });
4226
4242
 
4227
- const code$5 = "deleteSession";
4228
- async function handler$5(plugin, ctx, options) {
4243
+ const code$6 = "deleteSession";
4244
+ async function handler$6(plugin, ctx, options) {
4229
4245
  const { server, input, routerContext } = ctx;
4230
4246
  const { response } = routerContext;
4231
4247
  setCookie(response.headers, {
@@ -4238,12 +4254,12 @@ async function handler$5(plugin, ctx, options) {
4238
4254
 
4239
4255
  var deleteSession = /*#__PURE__*/Object.freeze({
4240
4256
  __proto__: null,
4241
- code: code$5,
4242
- handler: handler$5
4257
+ code: code$6,
4258
+ handler: handler$6
4243
4259
  });
4244
4260
 
4245
- const code$4 = "getMyProfile";
4246
- async function handler$4(plugin, ctx, options) {
4261
+ const code$5 = "getMyProfile";
4262
+ async function handler$5(plugin, ctx, options) {
4247
4263
  const { server, input, routerContext } = ctx;
4248
4264
  const userId = routerContext.state.userId;
4249
4265
  if (!userId) {
@@ -4272,6 +4288,39 @@ async function handler$4(plugin, ctx, options) {
4272
4288
  }
4273
4289
 
4274
4290
  var getMyProfile$2 = /*#__PURE__*/Object.freeze({
4291
+ __proto__: null,
4292
+ code: code$5,
4293
+ handler: handler$5
4294
+ });
4295
+
4296
+ const code$4 = "resetPassword";
4297
+ async function handler$4(plugin, ctx, options) {
4298
+ const { server, input, routerContext } = ctx;
4299
+ const { userId, password } = input;
4300
+ const userDataAccessor = server.getDataAccessor({
4301
+ singularCode: "oc_user",
4302
+ });
4303
+ const user = await userDataAccessor.findOne({
4304
+ filters: [
4305
+ {
4306
+ operator: "eq",
4307
+ field: "id",
4308
+ value: userId,
4309
+ }
4310
+ ]
4311
+ });
4312
+ if (!user) {
4313
+ throw new Error("User not found.");
4314
+ }
4315
+ const saltRounds = 10;
4316
+ const passwordHash = await bcrypt__default["default"].hash(password, saltRounds);
4317
+ await userDataAccessor.updateById(user.id, {
4318
+ password: passwordHash,
4319
+ });
4320
+ ctx.output = {};
4321
+ }
4322
+
4323
+ var resetPassword = /*#__PURE__*/Object.freeze({
4275
4324
  __proto__: null,
4276
4325
  code: code$4,
4277
4326
  handler: handler$4
@@ -4281,6 +4330,7 @@ var pluginActionHandlers$1 = [
4281
4330
  createSession,
4282
4331
  deleteSession,
4283
4332
  getMyProfile$2,
4333
+ resetPassword,
4284
4334
  ];
4285
4335
 
4286
4336
  var AccessToken = {
@@ -0,0 +1,4 @@
1
+ import { ActionHandlerContext } from "../../../core/actionHandler";
2
+ import { RapidPlugin } from "../../../core/server";
3
+ export declare const code = "changePassword";
4
+ export declare function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any): Promise<void>;
@@ -1,5 +1,6 @@
1
1
  import * as createSession from "./createSession";
2
2
  import * as deleteSession from "./deleteSession";
3
3
  import * as getMyProfile from "./getMyProfile";
4
- declare const _default: (typeof createSession | typeof deleteSession | typeof getMyProfile)[];
4
+ import * as resetPassword from "./resetPassword";
5
+ declare const _default: (typeof createSession | typeof deleteSession | typeof getMyProfile | typeof resetPassword)[];
5
6
  export default _default;
@@ -0,0 +1,4 @@
1
+ import { ActionHandlerContext } from "../../../core/actionHandler";
2
+ import { RapidPlugin } from "../../../core/server";
3
+ export declare const code = "resetPassword";
4
+ export declare function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any): Promise<void>;
package/dist/server.d.ts CHANGED
@@ -39,7 +39,7 @@ export declare class RapidServer implements IRpdServer {
39
39
  queryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>): Promise<any[]>;
40
40
  tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>): Promise<any[]>;
41
41
  get middlewares(): any[];
42
- handleRequest(request: Request, next: Next): any;
42
+ handleRequest(request: Request, next: Next): Promise<Response>;
43
43
  beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
44
44
  beforeCreateEntity(model: RpdDataModel, options: CreateEntityOptions): Promise<void>;
45
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.1.27",
3
+ "version": "0.1.28",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -18,6 +18,7 @@
18
18
  "typescript": "^4.8.4"
19
19
  },
20
20
  "dependencies": {
21
+ "bcrypt": "^5.1.1",
21
22
  "dayjs": "^1.11.7",
22
23
  "jsonwebtoken": "^9.0.2",
23
24
  "koa-tree-router": "^0.12.1",
@@ -0,0 +1,58 @@
1
+ import bcrypt from "bcrypt";
2
+ import { ActionHandlerContext } from "~/core/actionHandler";
3
+ import { RapidPlugin } from "~/core/server";
4
+
5
+ export const code = "changePassword";
6
+
7
+ export async function handler(
8
+ plugin: RapidPlugin,
9
+ ctx: ActionHandlerContext,
10
+ options: any,
11
+ ) {
12
+ const { server, input, routerContext } = ctx;
13
+ const { response } = routerContext;
14
+ const { id, oldPassword, newPassword } = input;
15
+
16
+ const userId = routerContext.state.userId;
17
+ if (!userId) {
18
+ ctx.status = 401;
19
+ ctx.output = {
20
+ error: {
21
+ message: "You are not signed in."
22
+ }
23
+ }
24
+ return;
25
+ }
26
+
27
+ const userDataAccessor = server.getDataAccessor({
28
+ singularCode: "oc_user",
29
+ });
30
+
31
+ const user = await userDataAccessor.findOne({
32
+ filters: [
33
+ {
34
+ operator: "eq",
35
+ field: "id",
36
+ value: userId,
37
+ }
38
+ ]
39
+ });
40
+
41
+ if (!user) {
42
+ throw new Error("User not found.");
43
+ }
44
+
45
+ const isMatch = await bcrypt.compare(oldPassword, user.password);
46
+ if (!isMatch) {
47
+ throw new Error("旧密码错误。");
48
+ }
49
+
50
+ const saltRounds = 10;
51
+ const passwordHash = await bcrypt.hash(newPassword, saltRounds);
52
+
53
+ await userDataAccessor.updateById(user.id, {
54
+ password: passwordHash,
55
+ });
56
+
57
+ ctx.output = {};
58
+ }
@@ -1,3 +1,4 @@
1
+ import bcrypt from "bcrypt";
1
2
  import { setCookie } from "~/deno-std/http/cookie";
2
3
  import { createJwt } from "~/utilities/jwtUtility";
3
4
  import { ActionHandlerContext } from "~/core/actionHandler";
@@ -34,7 +35,12 @@ export async function handler(
34
35
  });
35
36
 
36
37
  if (!user) {
37
- throw new Error("Wrong account or password.");
38
+ throw new Error("用户名或密码错误。");
39
+ }
40
+
41
+ const isMatch = await bcrypt.compare(password, user.password);
42
+ if (!isMatch) {
43
+ throw new Error("用户名或密码错误。");
38
44
  }
39
45
 
40
46
  const secretKey = Buffer.from(server.config.jwtKey, "base64");
@@ -2,9 +2,11 @@ import { IPluginActionHandler } from "~/core/actionHandler";
2
2
  import * as createSession from "./createSession";
3
3
  import * as deleteSession from "./deleteSession";
4
4
  import * as getMyProfile from "./getMyProfile";
5
+ import * as resetPassword from "./resetPassword";
5
6
 
6
7
  export default [
7
8
  createSession,
8
9
  deleteSession,
9
10
  getMyProfile,
11
+ resetPassword,
10
12
  ] satisfies IPluginActionHandler[];
@@ -0,0 +1,42 @@
1
+ import bcrypt from "bcrypt";
2
+ import { ActionHandlerContext } from "~/core/actionHandler";
3
+ import { RapidPlugin } from "~/core/server";
4
+
5
+ export const code = "resetPassword";
6
+
7
+ export async function handler(
8
+ plugin: RapidPlugin,
9
+ ctx: ActionHandlerContext,
10
+ options: any,
11
+ ) {
12
+ const { server, input, routerContext } = ctx;
13
+ const { response } = routerContext;
14
+ const { userId, password } = input;
15
+
16
+ const userDataAccessor = server.getDataAccessor({
17
+ singularCode: "oc_user",
18
+ });
19
+
20
+ const user = await userDataAccessor.findOne({
21
+ filters: [
22
+ {
23
+ operator: "eq",
24
+ field: "id",
25
+ value: userId,
26
+ }
27
+ ]
28
+ });
29
+
30
+ if (!user) {
31
+ throw new Error("User not found.");
32
+ }
33
+
34
+ const saltRounds = 10;
35
+ const passwordHash = await bcrypt.hash(password, saltRounds);
36
+
37
+ await userDataAccessor.updateById(user.id, {
38
+ password: passwordHash,
39
+ });
40
+
41
+ ctx.output = {};
42
+ }
package/src/server.ts CHANGED
@@ -312,10 +312,19 @@ export class RapidServer implements IRpdServer {
312
312
  async handleRequest(request: Request, next: Next) {
313
313
  const rapidRequest = new RapidRequest(this, request);
314
314
  await rapidRequest.parseBody();
315
- const routeContext = new RouteContext(this, rapidRequest);
316
- await this.#pluginManager.onPrepareRouteContext(routeContext);
315
+ const routeContext: RouteContext = new RouteContext(this, rapidRequest);
317
316
 
318
- await this.#buildedRoutes(routeContext, next);
317
+ try {
318
+ await this.#pluginManager.onPrepareRouteContext(routeContext);
319
+ await this.#buildedRoutes(routeContext, next);
320
+ } catch (ex) {
321
+ this.#logger.error('handle request error:', ex)
322
+ routeContext.response.json({
323
+ error: {
324
+ message: ex.message || ex,
325
+ },
326
+ }, 500);
327
+ }
319
328
  return routeContext.response.getResponse();
320
329
  }
321
330