propro-utils 1.3.24 → 1.3.25

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.
@@ -1,24 +1,27 @@
1
- require("dotenv").config()
1
+ require("dotenv").config();
2
2
  const axios = require("axios");
3
+ const {getOrSetCache} = require("../utils/redis");
3
4
 
4
- const authValidation = (requiredPermissions = []) => {
5
+ const authValidation = (redisClient, requiredPermissions = []) => {
5
6
  return async (req, res, next) => {
6
7
  try {
7
8
  const accessToken = req.cookies['x-access-token'] || req.headers.authorization?.split(" ")[1];
8
9
 
9
10
  if (!accessToken) {
10
- throw new Error('Access token is missing');
11
+ return res.status(403).json({ error: "Access token is required" });
11
12
  }
12
13
 
13
- const response = await axios.post(`${process.env.AUTH_URL}/api/v1/auth/validateToken`, {
14
- accessToken: accessToken,
15
- requiredPermissions: requiredPermissions
14
+ const response = await getOrSetCache(redisClient, accessToken, async () => {
15
+ return await axios.post(`${process.env.AUTH_URL}/api/v1/auth/validateToken`, {
16
+ accessToken: accessToken,
17
+ requiredPermissions: requiredPermissions
18
+ });
16
19
  });
17
- const { accountId, validPermissions } = response.data;
18
20
 
21
+ const { accountId, validPermissions } = response.data;
19
22
 
20
23
  if (!validPermissions) {
21
- throw new Error('Insufficient permissions');
24
+ return res.status(403).json({ error: "Invalid permissions" });
22
25
  }
23
26
 
24
27
  req.account = accountId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.3.24",
3
+ "version": "1.3.25",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -59,11 +59,12 @@
59
59
  "@react-email/tailwind": "^0.0.12",
60
60
  "@react-email/text": "0.0.5",
61
61
  "axios": "^1.6.1",
62
- "dotenv": "^16.3.1",
62
+ "dotenv": "^16.4.1",
63
63
  "express-rate-limit": "^7.1.4",
64
64
  "nodemailer": "^6.9.7",
65
65
  "nodemailer-mailgun-transport": "^2.1.5",
66
66
  "querystring": "^0.2.1",
67
- "react-email": "^1.9.5"
67
+ "react-email": "^1.9.5",
68
+ "redis": "^4.6.12"
68
69
  }
69
70
  }
package/utils/redis.js ADDED
@@ -0,0 +1,18 @@
1
+ const getOrSetCache = async (redisClient, key, service, time = 1800) => {
2
+ try {
3
+ const cachedValue = await redisClient.get(key);
4
+ if (cachedValue) {
5
+ console.log(`Cache hit for key: ${key}`);
6
+ return JSON.parse(cachedValue);
7
+ }
8
+ console.log(`Cache miss for key: ${key}. Fetching and caching value.`);
9
+ const value = await service();
10
+ await redisClient.setEx(key, time, JSON.stringify(value));
11
+ return value;
12
+ } catch (error) {
13
+ console.error(`Error in getOrSetCache for key ${key}:`, error);
14
+ throw error;
15
+ }
16
+ };
17
+
18
+ module.exports = { getOrSetCache };