propro-utils 1.3.24 → 1.3.26

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.26",
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
  }
@@ -49,7 +49,7 @@ function proproAuthMiddleware(options = {}) {
49
49
 
50
50
  console.log("code", code);
51
51
 
52
- const tokenData = await exchangeToken(
52
+ const {tokens, redirectUrl} = await exchangeToken(
53
53
  authUrl,
54
54
  code,
55
55
  clientId,
@@ -59,25 +59,25 @@ function proproAuthMiddleware(options = {}) {
59
59
 
60
60
  const currentDateTime = new Date();
61
61
 
62
- const refreshMaxAge = tokenData.tokens.refresh.expires.getTime() - currentDateTime.getTime();
62
+ const refreshMaxAge = new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
63
63
 
64
- res.cookie("x-refresh-token", tokenData.tokens.refresh.token, {
64
+ res.cookie("x-refresh-token", tokens.refresh.token, {
65
65
  httpOnly: true,
66
66
  secure: process.env.NODE_ENV === "production",
67
67
  maxAge: refreshMaxAge
68
68
  });
69
69
 
70
- const accessMaxAge = tokenData.tokens.access.expires.getTime() - currentDateTime.getTime();
70
+ const accessMaxAge = new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
71
71
 
72
- res.cookie("x-access-token", tokenData.tokens.access.token, {
72
+ res.cookie("x-access-token", tokens.access.token, {
73
73
  httpOnly: true,
74
74
  secure: process.env.NODE_ENV === "production",
75
75
  maxAge: accessMaxAge
76
76
  });
77
77
 
78
- const redirectUrl = `http://${tokenData.redirectUrl}/`;
78
+ const urlToRedirect = `http://${redirectUrl}/`;
79
79
 
80
- return res.redirect(redirectUrl);
80
+ return res.redirect(urlToRedirect);
81
81
  }
82
82
  } catch (error) {
83
83
  console.error("Error in proproAuthMiddleware:", error);
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 };