propro-utils 1.3.27 → 1.3.29

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, accessToken, fetchPermission, 1800);
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,61 @@
1
+ require("dotenv").config();
2
+ const axios = require("axios");
3
+ const {getOrSetCache} = require("../utils/redis");
4
+ const { v4: uuidv4 } = require('uuid');
5
+ const { Schema } = require('mongoose');
6
+ const { Client } = require('redis');
7
+ /**
8
+ * Retrieves the account profile data from the authentication server and caches it using Redis.
9
+ * If the profile data is not found in the cache, it fetches it from the authentication server and stores it in the cache.
10
+ *
11
+ * @param {Client} redisClient - Redis client instance
12
+ * @param {Schema} userSchema - User schema/model object
13
+ * @param {string} accountId - ID of the account
14
+ * @returns {Object} - Account profile data
15
+ * @throws {Error} - If there is an error retrieving the account profile data or validating the token
16
+ */
17
+ const getAccountProfile = async (redisClient, userSchema, accountId) => {
18
+ try {
19
+ const accessToken = req.cookies['x-access-token'] || req.headers.authorization?.split(" ")[1];
20
+
21
+ if (!accessToken) {
22
+ throw new Error("Access token is required");
23
+ }
24
+
25
+ const fetchPermission = async () => {
26
+ const response = await axios.get(`${process.env.AUTH_URL}/api/v1/account/profile`, {
27
+ headers: {
28
+ Authorization: `Bearer ${accessToken}`,
29
+ },
30
+ params: {
31
+ accountId
32
+ }
33
+ });
34
+ return response.data;
35
+ }
36
+ const cacheKey = `account:info:${accountId}`;
37
+ const { profileData } = await getOrSetCache(redisClient, cacheKey, fetchPermission, 1800);
38
+
39
+ if (!profileData) {
40
+ throw new Error("Invalid permissions");
41
+ }
42
+
43
+ // check if the user account is already in the database of the application that is using this middleware, if not, add it
44
+ const user = await userSchema.findOne({ id: profileData.id });
45
+ if (!user) {
46
+ await userSchema.create({
47
+ accountId: accountId
48
+ });
49
+ }
50
+
51
+ return profileData;
52
+
53
+ } catch (error) {
54
+ if (error.response && error.response.status) {
55
+ throw new Error(error.response.data.message);
56
+ }
57
+ throw new Error('Error validating token');
58
+ }
59
+ };
60
+
61
+ module.exports = getAccountProfile;
@@ -1,2 +1,3 @@
1
1
  module.exports.refreshAccessToken = require('./refresh_token');
2
- module.exports.authValidation = require("./access_token")
2
+ module.exports.authValidation = require("./access_token")
3
+ module.exports.getAccountProfile = require("./account_info")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.3.27",
3
+ "version": "1.3.29",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -61,10 +61,12 @@
61
61
  "axios": "^1.6.1",
62
62
  "dotenv": "^16.4.1",
63
63
  "express-rate-limit": "^7.1.4",
64
+ "mongoose": "^8.1.1",
64
65
  "nodemailer": "^6.9.7",
65
66
  "nodemailer-mailgun-transport": "^2.1.5",
66
67
  "querystring": "^0.2.1",
67
68
  "react-email": "^1.9.5",
68
- "redis": "^4.6.12"
69
+ "redis": "^4.6.12",
70
+ "uuid": "^9.0.1"
69
71
  }
70
72
  }