@7365admin1/core 2.34.0 → 2.35.0
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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +117 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +162 -66
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -62,6 +62,7 @@ type TUser = {
|
|
|
62
62
|
name: string;
|
|
63
63
|
defaultOrg?: string | ObjectId;
|
|
64
64
|
status?: string;
|
|
65
|
+
sid?: string;
|
|
65
66
|
createdAt?: string;
|
|
66
67
|
updatedAt?: string;
|
|
67
68
|
deletedAt?: string;
|
|
@@ -122,6 +123,12 @@ declare function useUserRepo(): {
|
|
|
122
123
|
}, session?: ClientSession) => Promise<string>;
|
|
123
124
|
updateDefaultOrgByEmail: (email: string, value: string, session?: ClientSession) => Promise<string>;
|
|
124
125
|
getUserByEmailStatus: (email: string) => Promise<TUser | null>;
|
|
126
|
+
updateUserSIDById: (id: string | ObjectId, sid: string, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
|
|
127
|
+
resetPassword: ({ _id, password, sid, }: {
|
|
128
|
+
_id: string | ObjectId;
|
|
129
|
+
password: string;
|
|
130
|
+
sid: string;
|
|
131
|
+
}, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
|
|
125
132
|
};
|
|
126
133
|
|
|
127
134
|
declare function useUserController(): {
|
|
@@ -6362,6 +6369,7 @@ declare function useAuthControllerV2(): {
|
|
|
6362
6369
|
login: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
6363
6370
|
logout: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
6364
6371
|
resetPassword: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
6372
|
+
verifyPassword: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
6365
6373
|
};
|
|
6366
6374
|
|
|
6367
6375
|
declare function useAuthServiceV2(): {
|
|
@@ -6375,6 +6383,7 @@ declare function useAuthServiceV2(): {
|
|
|
6375
6383
|
user: string;
|
|
6376
6384
|
}>;
|
|
6377
6385
|
logout: (sid: string) => Promise<string>;
|
|
6386
|
+
verifyPassword: (_id: string | ObjectId, password: string) => Promise<string>;
|
|
6378
6387
|
};
|
|
6379
6388
|
|
|
6380
6389
|
declare function useUserRepoV2(): {
|
package/dist/index.js
CHANGED
|
@@ -2605,20 +2605,9 @@ function useUserRepo() {
|
|
|
2605
2605
|
}
|
|
2606
2606
|
async function getUserByEmail(email) {
|
|
2607
2607
|
try {
|
|
2608
|
-
const cacheKey = (0, import_node_server_utils10.makeCacheKey)(namespace_collection, { email });
|
|
2609
|
-
const cachedData = await getCache(cacheKey);
|
|
2610
|
-
if (cachedData) {
|
|
2611
|
-
import_node_server_utils10.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
2612
|
-
return cachedData;
|
|
2613
|
-
}
|
|
2614
2608
|
const data = await collection.findOne({
|
|
2615
2609
|
email: { $regex: `^${email}$`, $options: "i" }
|
|
2616
2610
|
});
|
|
2617
|
-
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
2618
|
-
import_node_server_utils10.logger.info(`Cache set for key: ${cacheKey}`);
|
|
2619
|
-
}).catch((err) => {
|
|
2620
|
-
import_node_server_utils10.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
2621
|
-
});
|
|
2622
2611
|
return data;
|
|
2623
2612
|
} catch (error) {
|
|
2624
2613
|
throw new import_node_server_utils10.InternalServerError("Failed to get user by email.");
|
|
@@ -2925,6 +2914,39 @@ function useUserRepo() {
|
|
|
2925
2914
|
throw new import_node_server_utils10.InternalServerError("Failed to update user password.");
|
|
2926
2915
|
}
|
|
2927
2916
|
}
|
|
2917
|
+
async function resetPassword({
|
|
2918
|
+
_id,
|
|
2919
|
+
password,
|
|
2920
|
+
sid
|
|
2921
|
+
}, session) {
|
|
2922
|
+
try {
|
|
2923
|
+
_id = new import_mongodb9.ObjectId(_id);
|
|
2924
|
+
} catch (error) {
|
|
2925
|
+
throw new import_node_server_utils10.BadRequestError("Invalid user ID format.");
|
|
2926
|
+
}
|
|
2927
|
+
try {
|
|
2928
|
+
const result = await collection.updateOne(
|
|
2929
|
+
{ _id },
|
|
2930
|
+
{ $set: { password, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
|
|
2931
|
+
{ session }
|
|
2932
|
+
);
|
|
2933
|
+
const cacheKey = (0, import_node_server_utils10.makeCacheKey)(namespace_collection, { _id });
|
|
2934
|
+
delCache(cacheKey).then(() => {
|
|
2935
|
+
import_node_server_utils10.logger.info(`Cache deleted for key: ${cacheKey}`);
|
|
2936
|
+
}).catch((err) => {
|
|
2937
|
+
import_node_server_utils10.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
|
|
2938
|
+
});
|
|
2939
|
+
const authCacheKey = `sid:${sid}`;
|
|
2940
|
+
delCache(authCacheKey).then(() => {
|
|
2941
|
+
import_node_server_utils10.logger.info(`Cache deleted for key: ${authCacheKey}`);
|
|
2942
|
+
}).catch((err) => {
|
|
2943
|
+
import_node_server_utils10.logger.error(`Failed to delete cache for key: ${authCacheKey}`, err);
|
|
2944
|
+
});
|
|
2945
|
+
return result;
|
|
2946
|
+
} catch (error) {
|
|
2947
|
+
throw new import_node_server_utils10.InternalServerError("Failed to update user password.");
|
|
2948
|
+
}
|
|
2949
|
+
}
|
|
2928
2950
|
async function updateBirthday({
|
|
2929
2951
|
_id,
|
|
2930
2952
|
month,
|
|
@@ -3017,6 +3039,19 @@ function useUserRepo() {
|
|
|
3017
3039
|
throw new import_node_server_utils10.InternalServerError(`Failed to update user ${field}.`);
|
|
3018
3040
|
}
|
|
3019
3041
|
}
|
|
3042
|
+
async function updateUserSIDById(id, sid, session) {
|
|
3043
|
+
const _id = (0, import_node_server_utils10.toObjectId)(id);
|
|
3044
|
+
try {
|
|
3045
|
+
const result = await collection.updateOne(
|
|
3046
|
+
{ _id },
|
|
3047
|
+
{ $set: { sid, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
|
|
3048
|
+
{ session }
|
|
3049
|
+
);
|
|
3050
|
+
return result;
|
|
3051
|
+
} catch (error) {
|
|
3052
|
+
throw new import_node_server_utils10.InternalServerError("Failed to update user.");
|
|
3053
|
+
}
|
|
3054
|
+
}
|
|
3020
3055
|
return {
|
|
3021
3056
|
createIndex,
|
|
3022
3057
|
createTextIndex,
|
|
@@ -3032,7 +3067,9 @@ function useUserRepo() {
|
|
|
3032
3067
|
updateBirthday,
|
|
3033
3068
|
updateUserFieldById,
|
|
3034
3069
|
updateDefaultOrgByEmail,
|
|
3035
|
-
getUserByEmailStatus
|
|
3070
|
+
getUserByEmailStatus,
|
|
3071
|
+
updateUserSIDById,
|
|
3072
|
+
resetPassword
|
|
3036
3073
|
};
|
|
3037
3074
|
}
|
|
3038
3075
|
|
|
@@ -3860,7 +3897,11 @@ function useMemberRepo() {
|
|
|
3860
3897
|
|
|
3861
3898
|
// src/services/auth.service.ts
|
|
3862
3899
|
function useAuthService() {
|
|
3863
|
-
const {
|
|
3900
|
+
const {
|
|
3901
|
+
getUserByEmail,
|
|
3902
|
+
getUserById: _getUserById,
|
|
3903
|
+
updateUserSIDById: _updateUserSIDById
|
|
3904
|
+
} = useUserRepo();
|
|
3864
3905
|
const { getByToken, deleteByToken } = useSessionRepo();
|
|
3865
3906
|
const expiresIn = "15m";
|
|
3866
3907
|
const { setCache, delCache } = (0, import_node_server_utils13.useCache)("sessions");
|
|
@@ -3898,7 +3939,9 @@ function useAuthService() {
|
|
|
3898
3939
|
}
|
|
3899
3940
|
const sid = (0, import_uuid.v4)();
|
|
3900
3941
|
const cacheKey = `sid:${sid}`;
|
|
3901
|
-
|
|
3942
|
+
await _updateUserSIDById(user._id, sid);
|
|
3943
|
+
const updatedUser = await _getUserById(user._id);
|
|
3944
|
+
setCache(cacheKey, updatedUser, 14400).then(() => {
|
|
3902
3945
|
console.log("Session ID cached successfully");
|
|
3903
3946
|
}).catch((error) => {
|
|
3904
3947
|
console.error("Error caching session ID:", error);
|
|
@@ -6063,7 +6106,8 @@ function useUserService() {
|
|
|
6063
6106
|
getUserById,
|
|
6064
6107
|
getUserByEmail,
|
|
6065
6108
|
updatePassword,
|
|
6066
|
-
updateUserFieldById: _updateUserFieldById
|
|
6109
|
+
updateUserFieldById: _updateUserFieldById,
|
|
6110
|
+
resetPassword: _resetPassword
|
|
6067
6111
|
} = useUserRepo();
|
|
6068
6112
|
const { getRoleByName, addRole } = useRoleRepo();
|
|
6069
6113
|
const { add: addMember } = useMemberRepo();
|
|
@@ -6239,8 +6283,12 @@ function useUserService() {
|
|
|
6239
6283
|
throw new import_node_server_utils24.InternalServerError("Invalid user ID.");
|
|
6240
6284
|
}
|
|
6241
6285
|
await updateStatusById(id, "complete", session);
|
|
6242
|
-
await
|
|
6243
|
-
{
|
|
6286
|
+
await _resetPassword(
|
|
6287
|
+
{
|
|
6288
|
+
_id: user._id.toString(),
|
|
6289
|
+
password: hashedPassword,
|
|
6290
|
+
sid: user.sid
|
|
6291
|
+
},
|
|
6244
6292
|
session
|
|
6245
6293
|
);
|
|
6246
6294
|
await session?.commitTransaction();
|
|
@@ -8924,7 +8972,7 @@ var import_node_server_utils42 = require("@7365admin1/node-server-utils");
|
|
|
8924
8972
|
var import_zod = require("zod");
|
|
8925
8973
|
var import_mongodb26 = require("mongodb");
|
|
8926
8974
|
var import_node_server_utils41 = require("@7365admin1/node-server-utils");
|
|
8927
|
-
function
|
|
8975
|
+
function toObjectId4(value) {
|
|
8928
8976
|
if (typeof value === "string") {
|
|
8929
8977
|
if (!/^[a-fA-F0-9]{24}$/.test(value)) {
|
|
8930
8978
|
throw new import_node_server_utils41.BadRequestError(`Invalid ObjectId format: ${value}`);
|
|
@@ -8976,7 +9024,7 @@ var TInvoice = import_zod.z.object({
|
|
|
8976
9024
|
message: "Invalid ObjectId: Must be a 24-character hex string."
|
|
8977
9025
|
}),
|
|
8978
9026
|
import_zod.z.instanceof(import_mongodb26.ObjectId, { message: "Invalid ObjectId instance." })
|
|
8979
|
-
]).optional().transform((val) => val ?
|
|
9027
|
+
]).optional().transform((val) => val ? toObjectId4(val) : void 0),
|
|
8980
9028
|
invoiceNumber: import_zod.z.string({ required_error: "Invoice number is required." }),
|
|
8981
9029
|
type: TInvoiceType.default("other"),
|
|
8982
9030
|
amount: import_zod.z.number().min(0, { message: "Invoice amount must be at least 0." }),
|
|
@@ -48309,9 +48357,25 @@ function useAuthServiceV2() {
|
|
|
48309
48357
|
throw new import_node_server_utils222.InternalServerError("Error deleting token");
|
|
48310
48358
|
}
|
|
48311
48359
|
}
|
|
48360
|
+
async function verifyPassword(_id, password) {
|
|
48361
|
+
try {
|
|
48362
|
+
const user = await _getUserById(_id);
|
|
48363
|
+
const isPasswordMatch = await (0, import_node_server_utils222.comparePassword)(password, user.password);
|
|
48364
|
+
if (!isPasswordMatch) {
|
|
48365
|
+
throw new import_node_server_utils222.BadRequestError("Invalid credentials");
|
|
48366
|
+
}
|
|
48367
|
+
return "Verification successful.";
|
|
48368
|
+
} catch (error) {
|
|
48369
|
+
if (error instanceof import_node_server_utils222.BadRequestError) {
|
|
48370
|
+
throw error;
|
|
48371
|
+
}
|
|
48372
|
+
throw new import_node_server_utils222.InternalServerError("Error during password verification");
|
|
48373
|
+
}
|
|
48374
|
+
}
|
|
48312
48375
|
return {
|
|
48313
48376
|
login,
|
|
48314
|
-
logout
|
|
48377
|
+
logout,
|
|
48378
|
+
verifyPassword
|
|
48315
48379
|
};
|
|
48316
48380
|
}
|
|
48317
48381
|
|
|
@@ -48519,7 +48583,11 @@ function useUserServiceV2() {
|
|
|
48519
48583
|
// src/controllers/auth-v2.controller.ts
|
|
48520
48584
|
function useAuthControllerV2() {
|
|
48521
48585
|
const { signUp: _signUp } = useVerificationServiceV2();
|
|
48522
|
-
const {
|
|
48586
|
+
const {
|
|
48587
|
+
login: _login,
|
|
48588
|
+
logout: _logout,
|
|
48589
|
+
verifyPassword: _verifyPassword
|
|
48590
|
+
} = useAuthServiceV2();
|
|
48523
48591
|
const { resetPassword: _resetPassword } = useUserServiceV2();
|
|
48524
48592
|
async function signUp(req, res, next) {
|
|
48525
48593
|
try {
|
|
@@ -48633,11 +48701,38 @@ function useAuthControllerV2() {
|
|
|
48633
48701
|
return;
|
|
48634
48702
|
}
|
|
48635
48703
|
}
|
|
48704
|
+
async function verifyPassword(req, res, next) {
|
|
48705
|
+
const schema2 = import_joi127.default.object({
|
|
48706
|
+
_id: import_joi127.default.string().hex().required(),
|
|
48707
|
+
password: import_joi127.default.string().required().min(8)
|
|
48708
|
+
});
|
|
48709
|
+
const { error, value } = schema2.validate(
|
|
48710
|
+
{ _id: req.params.id, ...req.body },
|
|
48711
|
+
{ abortEarly: false }
|
|
48712
|
+
);
|
|
48713
|
+
if (error) {
|
|
48714
|
+
const messages = error.details.map((d) => d.message);
|
|
48715
|
+
import_node_server_utils224.logger.log({ level: "error", message: messages.join(", ") });
|
|
48716
|
+
next(new import_node_server_utils224.BadRequestError(messages.join(", ")));
|
|
48717
|
+
return;
|
|
48718
|
+
}
|
|
48719
|
+
const { _id, password } = value;
|
|
48720
|
+
try {
|
|
48721
|
+
const message = await _verifyPassword(_id, password);
|
|
48722
|
+
res.json({ message });
|
|
48723
|
+
return;
|
|
48724
|
+
} catch (error2) {
|
|
48725
|
+
import_node_server_utils224.logger.log({ level: "error", message: error2.message });
|
|
48726
|
+
next(error2);
|
|
48727
|
+
return;
|
|
48728
|
+
}
|
|
48729
|
+
}
|
|
48636
48730
|
return {
|
|
48637
48731
|
signUp,
|
|
48638
48732
|
login,
|
|
48639
48733
|
logout,
|
|
48640
|
-
resetPassword
|
|
48734
|
+
resetPassword,
|
|
48735
|
+
verifyPassword
|
|
48641
48736
|
};
|
|
48642
48737
|
}
|
|
48643
48738
|
|