vr-commons 1.0.80 → 1.0.82
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.
|
@@ -6,13 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.checkAdminAuthentication = exports.checkUserAuthentication = void 0;
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
8
|
const vr_models_1 = require("vr-models");
|
|
9
|
-
const
|
|
9
|
+
const response_utils_1 = require("../utils/response.utils");
|
|
10
10
|
const authTokens_utils_1 = require("../utils/authTokens.utils");
|
|
11
11
|
const checkUserAuthentication = (allowedRoles) => async (req, res, next) => {
|
|
12
12
|
try {
|
|
13
13
|
const authHeader = req.headers.authorization;
|
|
14
14
|
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
15
|
-
return (0,
|
|
15
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Missing or invalid token", 401);
|
|
16
16
|
}
|
|
17
17
|
const token = authHeader.split(" ")[1];
|
|
18
18
|
let payload;
|
|
@@ -22,9 +22,9 @@ const checkUserAuthentication = (allowedRoles) => async (req, res, next) => {
|
|
|
22
22
|
}
|
|
23
23
|
catch (error) {
|
|
24
24
|
if (error instanceof jsonwebtoken_1.default.TokenExpiredError) {
|
|
25
|
-
return (0,
|
|
25
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Token expired. Please re-verify your phone.", 401);
|
|
26
26
|
}
|
|
27
|
-
return (0,
|
|
27
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Token invalid", 401);
|
|
28
28
|
}
|
|
29
29
|
const user = await vr_models_1.User.findOne({
|
|
30
30
|
where: { id: payload.userId },
|
|
@@ -37,15 +37,15 @@ const checkUserAuthentication = (allowedRoles) => async (req, res, next) => {
|
|
|
37
37
|
});
|
|
38
38
|
// Check if user exists
|
|
39
39
|
if (!user || !user.securityClearance) {
|
|
40
|
-
return (0,
|
|
40
|
+
return (0, response_utils_1.sendErrorResponse)(res, "User not found", 400);
|
|
41
41
|
}
|
|
42
42
|
// 🔐 Token versioning (logout all devices)
|
|
43
43
|
if (user.tokenVersion !== payload.tokenVersion) {
|
|
44
|
-
return (0,
|
|
44
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Session expired. Please re-verify your phone.", 401);
|
|
45
45
|
}
|
|
46
46
|
// 🧱 Role enforcement
|
|
47
47
|
if (!allowedRoles.includes(user.securityClearance.role)) {
|
|
48
|
-
return (0,
|
|
48
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Access denied", 403);
|
|
49
49
|
}
|
|
50
50
|
// ✅ Extend req with user info
|
|
51
51
|
req.userId = user.id;
|
|
@@ -83,7 +83,7 @@ const checkAdminAuthentication = (allowedRoles) => async (req, res, next) => {
|
|
|
83
83
|
try {
|
|
84
84
|
const token = req.cookies?.access_token;
|
|
85
85
|
if (!token) {
|
|
86
|
-
return (0,
|
|
86
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Authentication required", 401);
|
|
87
87
|
}
|
|
88
88
|
let payload;
|
|
89
89
|
try {
|
|
@@ -91,9 +91,9 @@ const checkAdminAuthentication = (allowedRoles) => async (req, res, next) => {
|
|
|
91
91
|
}
|
|
92
92
|
catch (error) {
|
|
93
93
|
if (error instanceof jsonwebtoken_1.default.TokenExpiredError) {
|
|
94
|
-
return (0,
|
|
94
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Session expired. Please login again.", 401);
|
|
95
95
|
}
|
|
96
|
-
return (0,
|
|
96
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Invalid session", 401);
|
|
97
97
|
}
|
|
98
98
|
const user = await vr_models_1.User.findOne({
|
|
99
99
|
where: { id: payload.userId },
|
|
@@ -105,15 +105,15 @@ const checkAdminAuthentication = (allowedRoles) => async (req, res, next) => {
|
|
|
105
105
|
],
|
|
106
106
|
});
|
|
107
107
|
if (!user || !user.securityClearance) {
|
|
108
|
-
return (0,
|
|
108
|
+
return (0, response_utils_1.sendErrorResponse)(res, "User not found", 400);
|
|
109
109
|
}
|
|
110
110
|
// 🔐 Token versioning
|
|
111
111
|
if (user.tokenVersion !== payload.tokenVersion) {
|
|
112
|
-
return (0,
|
|
112
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Session expired", 401);
|
|
113
113
|
}
|
|
114
114
|
// 🧱 Role enforcement
|
|
115
115
|
if (!allowedRoles.includes(user.securityClearance.role)) {
|
|
116
|
-
return (0,
|
|
116
|
+
return (0, response_utils_1.sendErrorResponse)(res, "Access denied", 403);
|
|
117
117
|
}
|
|
118
118
|
// Attach user to request (same as before)
|
|
119
119
|
req.userId = user.id;
|
|
@@ -6,7 +6,7 @@ const sendSuccessResponse = (res, message, data = null, statusCode = 200) => {
|
|
|
6
6
|
res.status(statusCode).json({
|
|
7
7
|
success: true,
|
|
8
8
|
message,
|
|
9
|
-
data
|
|
9
|
+
data,
|
|
10
10
|
});
|
|
11
11
|
};
|
|
12
12
|
exports.sendSuccessResponse = sendSuccessResponse;
|
|
@@ -16,7 +16,7 @@ const sendErrorResponse = (res, message, statusCode = 400, error = null) => {
|
|
|
16
16
|
res.status(statusCode).json({
|
|
17
17
|
success: false,
|
|
18
18
|
message,
|
|
19
|
-
error: error
|
|
19
|
+
...(error && { error: error.message || error }),
|
|
20
20
|
});
|
|
21
21
|
};
|
|
22
22
|
exports.sendErrorResponse = sendErrorResponse;
|
|
@@ -18,7 +18,7 @@ export declare const createUserSchema: z.ZodObject<{
|
|
|
18
18
|
lastName: string;
|
|
19
19
|
password: string;
|
|
20
20
|
nationalId: string;
|
|
21
|
-
role: "
|
|
21
|
+
role: "RIDER" | "PASSENGER" | "ADMIN" | "AGENT" | "SUPER_ADMIN";
|
|
22
22
|
phoneNumber: string;
|
|
23
23
|
jacketId?: string | null | undefined;
|
|
24
24
|
email?: string | null | undefined;
|
|
@@ -28,7 +28,7 @@ export declare const createUserSchema: z.ZodObject<{
|
|
|
28
28
|
lastName: string;
|
|
29
29
|
password: string;
|
|
30
30
|
nationalId: string;
|
|
31
|
-
role: "
|
|
31
|
+
role: "RIDER" | "PASSENGER" | "ADMIN" | "AGENT" | "SUPER_ADMIN";
|
|
32
32
|
phoneNumber: string;
|
|
33
33
|
jacketId?: string | null | undefined;
|
|
34
34
|
email?: string | null | undefined;
|
|
@@ -47,7 +47,7 @@ export declare const createUserSchema: z.ZodObject<{
|
|
|
47
47
|
lastName: string;
|
|
48
48
|
password: string;
|
|
49
49
|
nationalId: string;
|
|
50
|
-
role: "
|
|
50
|
+
role: "RIDER" | "PASSENGER" | "ADMIN" | "AGENT" | "SUPER_ADMIN";
|
|
51
51
|
phoneNumber: string;
|
|
52
52
|
jacketId?: string | null | undefined;
|
|
53
53
|
email?: string | null | undefined;
|
|
@@ -62,7 +62,7 @@ export declare const createUserSchema: z.ZodObject<{
|
|
|
62
62
|
lastName: string;
|
|
63
63
|
password: string;
|
|
64
64
|
nationalId: string;
|
|
65
|
-
role: "
|
|
65
|
+
role: "RIDER" | "PASSENGER" | "ADMIN" | "AGENT" | "SUPER_ADMIN";
|
|
66
66
|
phoneNumber: string;
|
|
67
67
|
jacketId?: string | null | undefined;
|
|
68
68
|
email?: string | null | undefined;
|
|
@@ -224,7 +224,7 @@ export declare const getAllUsersSchema: z.ZodObject<{
|
|
|
224
224
|
search?: string | undefined;
|
|
225
225
|
limit?: number | undefined;
|
|
226
226
|
isActive?: boolean | undefined;
|
|
227
|
-
role?: "
|
|
227
|
+
role?: "RIDER" | "PASSENGER" | "ADMIN" | "AGENT" | "SUPER_ADMIN" | undefined;
|
|
228
228
|
isBanned?: boolean | undefined;
|
|
229
229
|
isSuspended?: boolean | undefined;
|
|
230
230
|
page?: number | undefined;
|
|
@@ -235,7 +235,7 @@ export declare const getAllUsersSchema: z.ZodObject<{
|
|
|
235
235
|
order?: "asc" | "desc" | undefined;
|
|
236
236
|
limit?: string | undefined;
|
|
237
237
|
isActive?: "true" | "false" | undefined;
|
|
238
|
-
role?: "
|
|
238
|
+
role?: "RIDER" | "PASSENGER" | "ADMIN" | "AGENT" | "SUPER_ADMIN" | undefined;
|
|
239
239
|
isBanned?: "true" | "false" | undefined;
|
|
240
240
|
isSuspended?: "true" | "false" | undefined;
|
|
241
241
|
page?: string | undefined;
|
|
@@ -257,7 +257,7 @@ export declare const getAllUsersSchema: z.ZodObject<{
|
|
|
257
257
|
search?: string | undefined;
|
|
258
258
|
limit?: number | undefined;
|
|
259
259
|
isActive?: boolean | undefined;
|
|
260
|
-
role?: "
|
|
260
|
+
role?: "RIDER" | "PASSENGER" | "ADMIN" | "AGENT" | "SUPER_ADMIN" | undefined;
|
|
261
261
|
isBanned?: boolean | undefined;
|
|
262
262
|
isSuspended?: boolean | undefined;
|
|
263
263
|
page?: number | undefined;
|
|
@@ -273,7 +273,7 @@ export declare const getAllUsersSchema: z.ZodObject<{
|
|
|
273
273
|
order?: "asc" | "desc" | undefined;
|
|
274
274
|
limit?: string | undefined;
|
|
275
275
|
isActive?: "true" | "false" | undefined;
|
|
276
|
-
role?: "
|
|
276
|
+
role?: "RIDER" | "PASSENGER" | "ADMIN" | "AGENT" | "SUPER_ADMIN" | undefined;
|
|
277
277
|
isBanned?: "true" | "false" | undefined;
|
|
278
278
|
isSuspended?: "true" | "false" | undefined;
|
|
279
279
|
page?: string | undefined;
|
|
@@ -9,7 +9,7 @@ const validate = (schema) => async (req, res, next) => {
|
|
|
9
9
|
body: req.body,
|
|
10
10
|
query: req.query,
|
|
11
11
|
params: req.params,
|
|
12
|
-
headers: req.headers
|
|
12
|
+
headers: req.headers,
|
|
13
13
|
});
|
|
14
14
|
// Store validated values in custom properties
|
|
15
15
|
req.validatedBody = result.body;
|
|
@@ -21,14 +21,16 @@ const validate = (schema) => async (req, res, next) => {
|
|
|
21
21
|
return next();
|
|
22
22
|
}
|
|
23
23
|
catch (error) {
|
|
24
|
-
console.error("
|
|
24
|
+
console.error("validate middleware function Error::::::", error);
|
|
25
25
|
if (error instanceof zod_1.ZodError) {
|
|
26
|
+
// Match the same format as sendErrorResponse
|
|
26
27
|
res.status(400).json({
|
|
27
|
-
|
|
28
|
+
success: false,
|
|
29
|
+
message: "Validation failed",
|
|
28
30
|
errors: error.errors.map((err) => ({
|
|
29
31
|
path: err.path.join("."),
|
|
30
|
-
message: err.message
|
|
31
|
-
}))
|
|
32
|
+
message: err.message,
|
|
33
|
+
})),
|
|
32
34
|
});
|
|
33
35
|
return;
|
|
34
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vr-commons",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.82",
|
|
4
4
|
"description": "Shared functions package",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
"date-fns": "^4.1.0",
|
|
101
101
|
"rimraf": "^5.0.5",
|
|
102
102
|
"typescript": "^5.3.3",
|
|
103
|
-
"vr-models": "^1.0.
|
|
103
|
+
"vr-models": "^1.0.51",
|
|
104
104
|
"zod": "^3.25.20"
|
|
105
105
|
},
|
|
106
106
|
"dependencies": {
|