@ruiapp/rapid-core 0.1.27 → 0.1.29
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 +212 -84
- package/dist/plugins/auth/actionHandlers/changePassword.d.ts +4 -0
- package/dist/plugins/auth/actionHandlers/index.d.ts +3 -1
- package/dist/plugins/auth/actionHandlers/resetPassword.d.ts +4 -0
- package/dist/plugins/auth/routes/changePassword.d.ts +12 -0
- package/dist/plugins/auth/routes/index.d.ts +2 -2
- package/dist/plugins/auth/routes/resetPassword.d.ts +12 -0
- package/dist/server.d.ts +1 -1
- package/package.json +2 -1
- package/src/plugins/auth/actionHandlers/changePassword.ts +58 -0
- package/src/plugins/auth/actionHandlers/createSession.ts +7 -1
- package/src/plugins/auth/actionHandlers/index.ts +4 -0
- package/src/plugins/auth/actionHandlers/resetPassword.ts +42 -0
- package/src/plugins/auth/routes/changePassword.ts +15 -0
- package/src/plugins/auth/routes/index.ts +4 -0
- package/src/plugins/auth/routes/resetPassword.ts +15 -0
- package/src/server.ts +12 -3
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
|
-
|
|
2617
|
-
|
|
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$
|
|
2776
|
-
async function handler$
|
|
2787
|
+
const code$n = "listMetaModels";
|
|
2788
|
+
async function handler$n(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$
|
|
2784
|
-
handler: handler$
|
|
2795
|
+
code: code$n,
|
|
2796
|
+
handler: handler$n
|
|
2785
2797
|
});
|
|
2786
2798
|
|
|
2787
|
-
const code$
|
|
2788
|
-
async function handler$
|
|
2799
|
+
const code$m = "listMetaRoutes";
|
|
2800
|
+
async function handler$m(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$
|
|
2796
|
-
handler: handler$
|
|
2807
|
+
code: code$m,
|
|
2808
|
+
handler: handler$m
|
|
2797
2809
|
});
|
|
2798
2810
|
|
|
2799
|
-
const code$
|
|
2800
|
-
async function handler$
|
|
2811
|
+
const code$l = "getMetaModelDetail";
|
|
2812
|
+
async function handler$l(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$
|
|
2809
|
-
handler: handler$
|
|
2820
|
+
code: code$l,
|
|
2821
|
+
handler: handler$l
|
|
2810
2822
|
});
|
|
2811
2823
|
|
|
2812
2824
|
/**
|
|
@@ -3158,9 +3170,9 @@ function transformFilterWithSubFilters(filter) {
|
|
|
3158
3170
|
return filter;
|
|
3159
3171
|
}
|
|
3160
3172
|
|
|
3161
|
-
const code$
|
|
3162
|
-
async function handler$
|
|
3163
|
-
await runCollectionEntityActionHandler(ctx, options, code$
|
|
3173
|
+
const code$k = "findCollectionEntities";
|
|
3174
|
+
async function handler$k(plugin, ctx, options) {
|
|
3175
|
+
await runCollectionEntityActionHandler(ctx, options, code$k, 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$
|
|
3179
|
-
handler: handler$
|
|
3190
|
+
code: code$k,
|
|
3191
|
+
handler: handler$k
|
|
3180
3192
|
});
|
|
3181
3193
|
|
|
3182
|
-
const code$
|
|
3183
|
-
async function handler$
|
|
3194
|
+
const code$j = "findCollectionEntityById";
|
|
3195
|
+
async function handler$j(plugin, ctx, options) {
|
|
3184
3196
|
const { logger, server, input } = ctx;
|
|
3185
|
-
logger.debug(`Running ${code$
|
|
3197
|
+
logger.debug(`Running ${code$j} 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$
|
|
3198
|
-
handler: handler$
|
|
3209
|
+
code: code$j,
|
|
3210
|
+
handler: handler$j
|
|
3199
3211
|
});
|
|
3200
3212
|
|
|
3201
|
-
const code$
|
|
3202
|
-
async function handler$
|
|
3203
|
-
await runCollectionEntityActionHandler(ctx, options, code$
|
|
3213
|
+
const code$i = "countCollectionEntities";
|
|
3214
|
+
async function handler$i(plugin, ctx, options) {
|
|
3215
|
+
await runCollectionEntityActionHandler(ctx, options, code$i, (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$
|
|
3212
|
-
handler: handler$
|
|
3223
|
+
code: code$i,
|
|
3224
|
+
handler: handler$i
|
|
3213
3225
|
});
|
|
3214
3226
|
|
|
3215
|
-
const code$
|
|
3216
|
-
async function handler$
|
|
3227
|
+
const code$h = "createCollectionEntity";
|
|
3228
|
+
async function handler$h(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$
|
|
3232
|
+
logger.debug(`Running ${code$h} 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$
|
|
3235
|
-
handler: handler$
|
|
3246
|
+
code: code$h,
|
|
3247
|
+
handler: handler$h
|
|
3236
3248
|
});
|
|
3237
3249
|
|
|
3238
|
-
const code$
|
|
3239
|
-
async function handler$
|
|
3250
|
+
const code$g = "createCollectionEntitiesBatch";
|
|
3251
|
+
async function handler$g(plugin, ctx, options) {
|
|
3240
3252
|
const { logger, server, input } = ctx;
|
|
3241
3253
|
const { defaultInput, fixedInput } = options;
|
|
3242
|
-
logger.debug(`Running ${code$
|
|
3254
|
+
logger.debug(`Running ${code$g} 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$
|
|
3266
|
-
handler: handler$
|
|
3277
|
+
code: code$g,
|
|
3278
|
+
handler: handler$g
|
|
3267
3279
|
});
|
|
3268
3280
|
|
|
3269
|
-
const code$
|
|
3270
|
-
async function handler$
|
|
3281
|
+
const code$f = "updateCollectionEntityById";
|
|
3282
|
+
async function handler$f(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$
|
|
3286
|
+
logger.debug(`Running ${code$f} 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$
|
|
3283
|
-
handler: handler$
|
|
3294
|
+
code: code$f,
|
|
3295
|
+
handler: handler$f
|
|
3284
3296
|
});
|
|
3285
3297
|
|
|
3286
|
-
const code$
|
|
3287
|
-
async function handler$
|
|
3298
|
+
const code$e = "deleteCollectionEntityById";
|
|
3299
|
+
async function handler$e(plugin, ctx, options) {
|
|
3288
3300
|
const { logger, server, input } = ctx;
|
|
3289
|
-
logger.debug(`Running ${code$
|
|
3301
|
+
logger.debug(`Running ${code$e} 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$
|
|
3299
|
-
handler: handler$
|
|
3310
|
+
code: code$e,
|
|
3311
|
+
handler: handler$e
|
|
3300
3312
|
});
|
|
3301
3313
|
|
|
3302
|
-
const code$
|
|
3303
|
-
async function handler$
|
|
3314
|
+
const code$d = "addEntityRelations";
|
|
3315
|
+
async function handler$d(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$
|
|
3319
|
+
logger.debug(`Running ${code$d} 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$
|
|
3316
|
-
handler: handler$
|
|
3327
|
+
code: code$d,
|
|
3328
|
+
handler: handler$d
|
|
3317
3329
|
});
|
|
3318
3330
|
|
|
3319
|
-
const code$
|
|
3320
|
-
async function handler$
|
|
3331
|
+
const code$c = "removeEntityRelations";
|
|
3332
|
+
async function handler$c(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$
|
|
3336
|
+
logger.debug(`Running ${code$c} 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$
|
|
3333
|
-
handler: handler$
|
|
3344
|
+
code: code$c,
|
|
3345
|
+
handler: handler$c
|
|
3334
3346
|
});
|
|
3335
3347
|
|
|
3336
|
-
const code$
|
|
3337
|
-
async function handler$
|
|
3348
|
+
const code$b = "queryDatabase";
|
|
3349
|
+
async function handler$b(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$
|
|
3353
|
+
logger.debug(`Running ${code$b} 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$
|
|
3354
|
-
handler: handler$
|
|
3365
|
+
code: code$b,
|
|
3366
|
+
handler: handler$b
|
|
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$
|
|
3526
|
-
async function handler$
|
|
3537
|
+
const code$a = "httpProxy";
|
|
3538
|
+
async function handler$a(plugin, ctx, options) {
|
|
3527
3539
|
const { logger } = ctx;
|
|
3528
|
-
logger.debug(`Running ${code$
|
|
3540
|
+
logger.debug(`Running ${code$a} 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$
|
|
3535
|
-
handler: handler$
|
|
3546
|
+
code: code$a,
|
|
3547
|
+
handler: handler$a
|
|
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$
|
|
3775
|
-
async function handler$
|
|
3786
|
+
const code$9 = "generateSn";
|
|
3787
|
+
async function handler$9(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$
|
|
3793
|
-
handler: handler$
|
|
3804
|
+
code: code$9,
|
|
3805
|
+
handler: handler$9
|
|
3794
3806
|
});
|
|
3795
3807
|
|
|
3796
3808
|
var pluginActionHandlers$2 = [
|
|
@@ -4180,8 +4192,55 @@ class WebhooksPlugin {
|
|
|
4180
4192
|
}
|
|
4181
4193
|
}
|
|
4182
4194
|
|
|
4183
|
-
const code$
|
|
4184
|
-
async function handler$
|
|
4195
|
+
const code$8 = "changePassword";
|
|
4196
|
+
async function handler$8(plugin, ctx, options) {
|
|
4197
|
+
const { server, input, routerContext } = ctx;
|
|
4198
|
+
const { id, oldPassword, newPassword } = input;
|
|
4199
|
+
const userId = routerContext.state.userId;
|
|
4200
|
+
if (!userId) {
|
|
4201
|
+
ctx.status = 401;
|
|
4202
|
+
ctx.output = {
|
|
4203
|
+
error: {
|
|
4204
|
+
message: "You are not signed in."
|
|
4205
|
+
}
|
|
4206
|
+
};
|
|
4207
|
+
return;
|
|
4208
|
+
}
|
|
4209
|
+
const userDataAccessor = server.getDataAccessor({
|
|
4210
|
+
singularCode: "oc_user",
|
|
4211
|
+
});
|
|
4212
|
+
const user = await userDataAccessor.findOne({
|
|
4213
|
+
filters: [
|
|
4214
|
+
{
|
|
4215
|
+
operator: "eq",
|
|
4216
|
+
field: "id",
|
|
4217
|
+
value: userId,
|
|
4218
|
+
}
|
|
4219
|
+
]
|
|
4220
|
+
});
|
|
4221
|
+
if (!user) {
|
|
4222
|
+
throw new Error("User not found.");
|
|
4223
|
+
}
|
|
4224
|
+
const isMatch = await bcrypt__default["default"].compare(oldPassword, user.password);
|
|
4225
|
+
if (!isMatch) {
|
|
4226
|
+
throw new Error("旧密码错误。");
|
|
4227
|
+
}
|
|
4228
|
+
const saltRounds = 10;
|
|
4229
|
+
const passwordHash = await bcrypt__default["default"].hash(newPassword, saltRounds);
|
|
4230
|
+
await userDataAccessor.updateById(user.id, {
|
|
4231
|
+
password: passwordHash,
|
|
4232
|
+
});
|
|
4233
|
+
ctx.output = {};
|
|
4234
|
+
}
|
|
4235
|
+
|
|
4236
|
+
var changePassword$1 = /*#__PURE__*/Object.freeze({
|
|
4237
|
+
__proto__: null,
|
|
4238
|
+
code: code$8,
|
|
4239
|
+
handler: handler$8
|
|
4240
|
+
});
|
|
4241
|
+
|
|
4242
|
+
const code$7 = "createSession";
|
|
4243
|
+
async function handler$7(plugin, ctx, options) {
|
|
4185
4244
|
const { server, input, routerContext } = ctx;
|
|
4186
4245
|
const { response } = routerContext;
|
|
4187
4246
|
const { account, password } = input;
|
|
@@ -4198,7 +4257,11 @@ async function handler$6(plugin, ctx, options) {
|
|
|
4198
4257
|
]
|
|
4199
4258
|
});
|
|
4200
4259
|
if (!user) {
|
|
4201
|
-
throw new Error("
|
|
4260
|
+
throw new Error("用户名或密码错误。");
|
|
4261
|
+
}
|
|
4262
|
+
const isMatch = await bcrypt__default["default"].compare(password, user.password);
|
|
4263
|
+
if (!isMatch) {
|
|
4264
|
+
throw new Error("用户名或密码错误。");
|
|
4202
4265
|
}
|
|
4203
4266
|
const secretKey = Buffer.from(server.config.jwtKey, "base64");
|
|
4204
4267
|
const token = createJwt({
|
|
@@ -4220,12 +4283,12 @@ async function handler$6(plugin, ctx, options) {
|
|
|
4220
4283
|
|
|
4221
4284
|
var createSession = /*#__PURE__*/Object.freeze({
|
|
4222
4285
|
__proto__: null,
|
|
4223
|
-
code: code$
|
|
4224
|
-
handler: handler$
|
|
4286
|
+
code: code$7,
|
|
4287
|
+
handler: handler$7
|
|
4225
4288
|
});
|
|
4226
4289
|
|
|
4227
|
-
const code$
|
|
4228
|
-
async function handler$
|
|
4290
|
+
const code$6 = "deleteSession";
|
|
4291
|
+
async function handler$6(plugin, ctx, options) {
|
|
4229
4292
|
const { server, input, routerContext } = ctx;
|
|
4230
4293
|
const { response } = routerContext;
|
|
4231
4294
|
setCookie(response.headers, {
|
|
@@ -4238,12 +4301,12 @@ async function handler$5(plugin, ctx, options) {
|
|
|
4238
4301
|
|
|
4239
4302
|
var deleteSession = /*#__PURE__*/Object.freeze({
|
|
4240
4303
|
__proto__: null,
|
|
4241
|
-
code: code$
|
|
4242
|
-
handler: handler$
|
|
4304
|
+
code: code$6,
|
|
4305
|
+
handler: handler$6
|
|
4243
4306
|
});
|
|
4244
4307
|
|
|
4245
|
-
const code$
|
|
4246
|
-
async function handler$
|
|
4308
|
+
const code$5 = "getMyProfile";
|
|
4309
|
+
async function handler$5(plugin, ctx, options) {
|
|
4247
4310
|
const { server, input, routerContext } = ctx;
|
|
4248
4311
|
const userId = routerContext.state.userId;
|
|
4249
4312
|
if (!userId) {
|
|
@@ -4272,15 +4335,50 @@ async function handler$4(plugin, ctx, options) {
|
|
|
4272
4335
|
}
|
|
4273
4336
|
|
|
4274
4337
|
var getMyProfile$2 = /*#__PURE__*/Object.freeze({
|
|
4338
|
+
__proto__: null,
|
|
4339
|
+
code: code$5,
|
|
4340
|
+
handler: handler$5
|
|
4341
|
+
});
|
|
4342
|
+
|
|
4343
|
+
const code$4 = "resetPassword";
|
|
4344
|
+
async function handler$4(plugin, ctx, options) {
|
|
4345
|
+
const { server, input, routerContext } = ctx;
|
|
4346
|
+
const { userId, password } = input;
|
|
4347
|
+
const userDataAccessor = server.getDataAccessor({
|
|
4348
|
+
singularCode: "oc_user",
|
|
4349
|
+
});
|
|
4350
|
+
const user = await userDataAccessor.findOne({
|
|
4351
|
+
filters: [
|
|
4352
|
+
{
|
|
4353
|
+
operator: "eq",
|
|
4354
|
+
field: "id",
|
|
4355
|
+
value: userId,
|
|
4356
|
+
}
|
|
4357
|
+
]
|
|
4358
|
+
});
|
|
4359
|
+
if (!user) {
|
|
4360
|
+
throw new Error("User not found.");
|
|
4361
|
+
}
|
|
4362
|
+
const saltRounds = 10;
|
|
4363
|
+
const passwordHash = await bcrypt__default["default"].hash(password, saltRounds);
|
|
4364
|
+
await userDataAccessor.updateById(user.id, {
|
|
4365
|
+
password: passwordHash,
|
|
4366
|
+
});
|
|
4367
|
+
ctx.output = {};
|
|
4368
|
+
}
|
|
4369
|
+
|
|
4370
|
+
var resetPassword$1 = /*#__PURE__*/Object.freeze({
|
|
4275
4371
|
__proto__: null,
|
|
4276
4372
|
code: code$4,
|
|
4277
4373
|
handler: handler$4
|
|
4278
4374
|
});
|
|
4279
4375
|
|
|
4280
4376
|
var pluginActionHandlers$1 = [
|
|
4377
|
+
changePassword$1,
|
|
4281
4378
|
createSession,
|
|
4282
4379
|
deleteSession,
|
|
4283
4380
|
getMyProfile$2,
|
|
4381
|
+
resetPassword$1,
|
|
4284
4382
|
];
|
|
4285
4383
|
|
|
4286
4384
|
var AccessToken = {
|
|
@@ -4342,6 +4440,20 @@ var pluginModels = [
|
|
|
4342
4440
|
AccessToken,
|
|
4343
4441
|
];
|
|
4344
4442
|
|
|
4443
|
+
var changePassword = {
|
|
4444
|
+
namespace: "auth",
|
|
4445
|
+
name: "auth.changePassword",
|
|
4446
|
+
code: "auth.changePassword",
|
|
4447
|
+
type: "RESTful",
|
|
4448
|
+
method: "POST",
|
|
4449
|
+
endpoint: "/changePassword",
|
|
4450
|
+
actions: [
|
|
4451
|
+
{
|
|
4452
|
+
code: "changePassword",
|
|
4453
|
+
},
|
|
4454
|
+
],
|
|
4455
|
+
};
|
|
4456
|
+
|
|
4345
4457
|
var getMyProfile$1 = {
|
|
4346
4458
|
namespace: "auth",
|
|
4347
4459
|
name: "auth.getMyProfile",
|
|
@@ -4356,6 +4468,20 @@ var getMyProfile$1 = {
|
|
|
4356
4468
|
],
|
|
4357
4469
|
};
|
|
4358
4470
|
|
|
4471
|
+
var resetPassword = {
|
|
4472
|
+
namespace: "auth",
|
|
4473
|
+
name: "auth.resetPassword",
|
|
4474
|
+
code: "auth.resetPassword",
|
|
4475
|
+
type: "RESTful",
|
|
4476
|
+
method: "POST",
|
|
4477
|
+
endpoint: "/resetPassword",
|
|
4478
|
+
actions: [
|
|
4479
|
+
{
|
|
4480
|
+
code: "resetPassword",
|
|
4481
|
+
},
|
|
4482
|
+
],
|
|
4483
|
+
};
|
|
4484
|
+
|
|
4359
4485
|
var signin$1 = {
|
|
4360
4486
|
namespace: "auth",
|
|
4361
4487
|
name: "auth.signin",
|
|
@@ -4385,7 +4511,9 @@ var signout$1 = {
|
|
|
4385
4511
|
};
|
|
4386
4512
|
|
|
4387
4513
|
var pluginRoutes$1 = [
|
|
4514
|
+
changePassword,
|
|
4388
4515
|
getMyProfile$1,
|
|
4516
|
+
resetPassword,
|
|
4389
4517
|
signin$1,
|
|
4390
4518
|
signout$1,
|
|
4391
4519
|
];
|
|
@@ -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,7 @@
|
|
|
1
|
+
import * as changePassword from "./changePassword";
|
|
1
2
|
import * as createSession from "./createSession";
|
|
2
3
|
import * as deleteSession from "./deleteSession";
|
|
3
4
|
import * as getMyProfile from "./getMyProfile";
|
|
4
|
-
|
|
5
|
+
import * as resetPassword from "./resetPassword";
|
|
6
|
+
declare const _default: (typeof changePassword | typeof createSession | typeof deleteSession | typeof getMyProfile | typeof resetPassword)[];
|
|
5
7
|
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>;
|
|
@@ -3,7 +3,7 @@ declare const _default: ({
|
|
|
3
3
|
name: string;
|
|
4
4
|
code: string;
|
|
5
5
|
type: "RESTful";
|
|
6
|
-
method: "
|
|
6
|
+
method: "POST";
|
|
7
7
|
endpoint: string;
|
|
8
8
|
actions: {
|
|
9
9
|
code: string;
|
|
@@ -13,7 +13,7 @@ declare const _default: ({
|
|
|
13
13
|
name: string;
|
|
14
14
|
code: string;
|
|
15
15
|
type: "RESTful";
|
|
16
|
-
method: "
|
|
16
|
+
method: "GET";
|
|
17
17
|
endpoint: string;
|
|
18
18
|
actions: {
|
|
19
19
|
code: string;
|
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):
|
|
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.
|
|
3
|
+
"version": "0.1.29",
|
|
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("
|
|
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");
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { IPluginActionHandler } from "~/core/actionHandler";
|
|
2
|
+
import * as changePassword from "./changePassword";
|
|
2
3
|
import * as createSession from "./createSession";
|
|
3
4
|
import * as deleteSession from "./deleteSession";
|
|
4
5
|
import * as getMyProfile from "./getMyProfile";
|
|
6
|
+
import * as resetPassword from "./resetPassword";
|
|
5
7
|
|
|
6
8
|
export default [
|
|
9
|
+
changePassword,
|
|
7
10
|
createSession,
|
|
8
11
|
deleteSession,
|
|
9
12
|
getMyProfile,
|
|
13
|
+
resetPassword,
|
|
10
14
|
] 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
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RpdRoute } from "~/types";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
namespace: "auth",
|
|
5
|
+
name: "auth.changePassword",
|
|
6
|
+
code: "auth.changePassword",
|
|
7
|
+
type: "RESTful",
|
|
8
|
+
method: "POST",
|
|
9
|
+
endpoint: "/changePassword",
|
|
10
|
+
actions: [
|
|
11
|
+
{
|
|
12
|
+
code: "changePassword",
|
|
13
|
+
},
|
|
14
|
+
],
|
|
15
|
+
} satisfies RpdRoute;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import changePassword from "./changePassword";
|
|
1
2
|
import getMyProfile from "./getMyProfile";
|
|
3
|
+
import resetPassword from "./resetPassword";
|
|
2
4
|
import signin from "./signin";
|
|
3
5
|
import signout from "./signout";
|
|
4
6
|
|
|
5
7
|
export default [
|
|
8
|
+
changePassword,
|
|
6
9
|
getMyProfile,
|
|
10
|
+
resetPassword,
|
|
7
11
|
signin,
|
|
8
12
|
signout,
|
|
9
13
|
]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RpdRoute } from "~/types";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
namespace: "auth",
|
|
5
|
+
name: "auth.resetPassword",
|
|
6
|
+
code: "auth.resetPassword",
|
|
7
|
+
type: "RESTful",
|
|
8
|
+
method: "POST",
|
|
9
|
+
endpoint: "/resetPassword",
|
|
10
|
+
actions: [
|
|
11
|
+
{
|
|
12
|
+
code: "resetPassword",
|
|
13
|
+
},
|
|
14
|
+
],
|
|
15
|
+
} satisfies RpdRoute;
|
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
|
-
|
|
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
|
|