@sendhome/common 1.0.278 → 1.0.280
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/build/events/publishers/order-created-event.d.ts +9 -0
- package/build/events/publishers/order-updated-event.d.ts +9 -0
- package/build/middlewares/current-user.d.ts +1 -1
- package/build/middlewares/current-user.js +19 -3
- package/build/middlewares/require-auth.js +33 -24
- package/package.json +1 -1
|
@@ -2,6 +2,12 @@ import { Subjects } from "../subjects/subjects";
|
|
|
2
2
|
import { OrderStatus } from "../types/order-status";
|
|
3
3
|
import { Category } from "../types/category";
|
|
4
4
|
type QuoteStatus = "requested" | "amended" | "approved" | "rejected" | "expired";
|
|
5
|
+
type ContactSnapshot = {
|
|
6
|
+
name?: string;
|
|
7
|
+
email?: string;
|
|
8
|
+
phone?: string;
|
|
9
|
+
address?: string;
|
|
10
|
+
};
|
|
5
11
|
export interface OrderCreatedEvent {
|
|
6
12
|
subject: Subjects.OrderCreated;
|
|
7
13
|
data: {
|
|
@@ -34,6 +40,9 @@ export interface OrderCreatedEvent {
|
|
|
34
40
|
delivery_address: string;
|
|
35
41
|
payment_method: "card" | "paypal" | "applePay" | "bankTransfer";
|
|
36
42
|
sender: string;
|
|
43
|
+
guest_session_id?: string;
|
|
44
|
+
guest_contact?: ContactSnapshot;
|
|
45
|
+
recipient_contact?: ContactSnapshot;
|
|
37
46
|
receiver: string;
|
|
38
47
|
module: string;
|
|
39
48
|
company: string;
|
|
@@ -2,6 +2,12 @@ import { Subjects } from "../subjects/subjects";
|
|
|
2
2
|
import { OrderStatus } from "../types/order-status";
|
|
3
3
|
import { Category } from "../types/category";
|
|
4
4
|
type QuoteStatus = "requested" | "amended" | "approved" | "rejected" | "expired";
|
|
5
|
+
type ContactSnapshot = {
|
|
6
|
+
name?: string;
|
|
7
|
+
email?: string;
|
|
8
|
+
phone?: string;
|
|
9
|
+
address?: string;
|
|
10
|
+
};
|
|
5
11
|
export interface OrderUpdatedEvent {
|
|
6
12
|
subject: Subjects.OrderUpdated;
|
|
7
13
|
data: {
|
|
@@ -34,6 +40,9 @@ export interface OrderUpdatedEvent {
|
|
|
34
40
|
delivery_address: string;
|
|
35
41
|
payment_method: "card" | "paypal" | "applePay" | "bankTransfer";
|
|
36
42
|
sender: string;
|
|
43
|
+
guest_session_id?: string;
|
|
44
|
+
guest_contact?: ContactSnapshot;
|
|
45
|
+
recipient_contact?: ContactSnapshot;
|
|
37
46
|
receiver: string;
|
|
38
47
|
module: string;
|
|
39
48
|
company: string;
|
|
@@ -5,13 +5,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.currentUser = void 0;
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
-
const
|
|
8
|
+
const extractAccessToken = (req) => {
|
|
9
9
|
var _a;
|
|
10
|
-
|
|
10
|
+
const cookieToken = (_a = req.cookies) === null || _a === void 0 ? void 0 : _a.token;
|
|
11
|
+
if (cookieToken) {
|
|
12
|
+
return cookieToken;
|
|
13
|
+
}
|
|
14
|
+
const authHeader = req.headers.authorization;
|
|
15
|
+
if (!authHeader) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
const [scheme, token] = authHeader.split(" ");
|
|
19
|
+
if (!token || (scheme === null || scheme === void 0 ? void 0 : scheme.toLowerCase()) !== "bearer") {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
return token;
|
|
23
|
+
};
|
|
24
|
+
const currentUser = (req, res, next) => {
|
|
25
|
+
const token = extractAccessToken(req);
|
|
26
|
+
if (!token) {
|
|
11
27
|
return next();
|
|
12
28
|
}
|
|
13
29
|
try {
|
|
14
|
-
const payload = jsonwebtoken_1.default.verify(
|
|
30
|
+
const payload = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY || "EDWIN");
|
|
15
31
|
req.currentUser = payload;
|
|
16
32
|
}
|
|
17
33
|
catch (err) { }
|
|
@@ -15,53 +15,62 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.requireAuth = void 0;
|
|
16
16
|
const axios_1 = __importDefault(require("axios"));
|
|
17
17
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
18
|
+
const extractAccessToken = (req) => {
|
|
19
|
+
var _a;
|
|
20
|
+
const cookieToken = (_a = req.cookies) === null || _a === void 0 ? void 0 : _a.token;
|
|
21
|
+
if (cookieToken) {
|
|
22
|
+
return cookieToken;
|
|
23
|
+
}
|
|
24
|
+
const authHeader = req.headers.authorization;
|
|
25
|
+
if (!authHeader) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const [scheme, token] = authHeader.split(" ");
|
|
29
|
+
if (!token || (scheme === null || scheme === void 0 ? void 0 : scheme.toLowerCase()) !== "bearer") {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return token;
|
|
33
|
+
};
|
|
18
34
|
// Middleware to validate user session per device
|
|
19
35
|
const requireAuth = (sessionServiceUrl) => {
|
|
20
36
|
return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
37
|
var _a, _b;
|
|
22
|
-
console.log("req.cookies.token", req.cookies.token);
|
|
23
38
|
try {
|
|
24
|
-
const token =
|
|
25
|
-
console.log("req.headers.authorization", (_b = req.headers.authorization) === null || _b === void 0 ? void 0 : _b.split(" ")[1]);
|
|
39
|
+
const token = extractAccessToken(req);
|
|
26
40
|
if (!token) {
|
|
27
41
|
res.status(401).json({ message: "No session token provided" });
|
|
28
42
|
return;
|
|
29
43
|
}
|
|
30
|
-
|
|
31
|
-
const payload = jsonwebtoken_1.default.verify(token, "EDWIN"
|
|
32
|
-
// process.env.JWT_KEY!
|
|
33
|
-
);
|
|
34
|
-
console.log("session service url", `${sessionServiceUrl}/${payload.sessionId}`);
|
|
44
|
+
const payload = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY || "EDWIN");
|
|
35
45
|
// Call the session service to validate the session
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// Attach user ID to request object
|
|
42
|
-
req.currentUser = response.data;
|
|
46
|
+
yield axios_1.default.get(`${sessionServiceUrl}/${payload.sessionId}`, {
|
|
47
|
+
timeout: 5000,
|
|
48
|
+
});
|
|
49
|
+
// Keep the verified JWT payload as the canonical currentUser data.
|
|
50
|
+
req.currentUser = payload;
|
|
43
51
|
next();
|
|
44
52
|
}
|
|
45
53
|
catch (error) {
|
|
46
|
-
console.error("Error during session validation:", error);
|
|
47
|
-
// Check if error is an Axios error (e.g., network failure or service unreachable)
|
|
48
54
|
if (axios_1.default.isAxiosError(error)) {
|
|
49
|
-
|
|
50
|
-
.status(
|
|
51
|
-
|
|
55
|
+
if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 || ((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) === 404) {
|
|
56
|
+
res.status(401).json({ message: "Invalid or expired session" });
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
res.status(500).json({
|
|
52
60
|
message: "Error connecting to session service",
|
|
53
61
|
error: error.message,
|
|
54
62
|
});
|
|
55
63
|
}
|
|
56
64
|
else if (error.name === "JsonWebTokenError") {
|
|
57
|
-
res
|
|
58
|
-
|
|
59
|
-
|
|
65
|
+
res.status(401).json({ message: "Invalid access token" });
|
|
66
|
+
}
|
|
67
|
+
else if (error.name === "TokenExpiredError") {
|
|
68
|
+
res.status(401).json({ message: "Access token expired" });
|
|
60
69
|
}
|
|
61
70
|
else {
|
|
62
71
|
res
|
|
63
72
|
.status(500)
|
|
64
|
-
.json({ message: "Server error during session validation"
|
|
73
|
+
.json({ message: "Server error during session validation" });
|
|
65
74
|
}
|
|
66
75
|
}
|
|
67
76
|
});
|