propro-utils 1.3.27 → 1.3.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.
|
@@ -18,8 +18,8 @@ const authValidation = (redisClient, requiredPermissions = []) => {
|
|
|
18
18
|
});
|
|
19
19
|
return response.data;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
const { accountId, validPermissions } = await getOrSetCache(redisClient,
|
|
21
|
+
const cacheKey = `account:permissions:${accessToken}`;
|
|
22
|
+
const { accountId, validPermissions } = await getOrSetCache(redisClient, cacheKey, fetchPermission, 1800);
|
|
23
23
|
|
|
24
24
|
if (!validPermissions) {
|
|
25
25
|
return res.status(403).json({ error: "Invalid permissions" });
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require("dotenv").config();
|
|
2
|
+
const axios = require("axios");
|
|
3
|
+
const {getOrSetCache} = require("../utils/redis");
|
|
4
|
+
|
|
5
|
+
const getAccountProfile = async (redisClient, accountId) => {
|
|
6
|
+
try {
|
|
7
|
+
const accessToken = req.cookies['x-access-token'] || req.headers.authorization?.split(" ")[1];
|
|
8
|
+
|
|
9
|
+
if (!accessToken) {
|
|
10
|
+
throw new Error("Access token is required");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const fetchPermission = async () => {
|
|
14
|
+
const response = await axios.get(`${process.env.AUTH_URL}/api/v1/account/profile`, {
|
|
15
|
+
headers: {
|
|
16
|
+
Authorization: `Bearer ${accessToken}`,
|
|
17
|
+
},
|
|
18
|
+
params: {
|
|
19
|
+
accountId
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return response.data;
|
|
23
|
+
}
|
|
24
|
+
const cacheKey = `account:info:${accountId}`;
|
|
25
|
+
const { profileData } = await getOrSetCache(redisClient, cacheKey, fetchPermission, 1800);
|
|
26
|
+
|
|
27
|
+
if (!profileData) {
|
|
28
|
+
throw new Error("Invalid permissions");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return profileData;
|
|
32
|
+
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (error.response && error.response.status) {
|
|
35
|
+
next(new Error(error.response.data.message));
|
|
36
|
+
}
|
|
37
|
+
throw new Error('Error validating token');
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
module.exports = getAccountProfile;
|
package/middlewares/index.js
CHANGED