propro-utils 1.3.28 → 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.
- package/middlewares/account_info.js +23 -3
- package/package.json +4 -2
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
require("dotenv").config();
|
|
2
2
|
const axios = require("axios");
|
|
3
3
|
const {getOrSetCache} = require("../utils/redis");
|
|
4
|
-
|
|
5
|
-
const
|
|
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) => {
|
|
6
18
|
try {
|
|
7
19
|
const accessToken = req.cookies['x-access-token'] || req.headers.authorization?.split(" ")[1];
|
|
8
20
|
|
|
@@ -28,11 +40,19 @@ const getAccountProfile = async (redisClient, accountId) => {
|
|
|
28
40
|
throw new Error("Invalid permissions");
|
|
29
41
|
}
|
|
30
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
|
+
|
|
31
51
|
return profileData;
|
|
32
52
|
|
|
33
53
|
} catch (error) {
|
|
34
54
|
if (error.response && error.response.status) {
|
|
35
|
-
|
|
55
|
+
throw new Error(error.response.data.message);
|
|
36
56
|
}
|
|
37
57
|
throw new Error('Error validating token');
|
|
38
58
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "propro-utils",
|
|
3
|
-
"version": "1.3.
|
|
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
|
}
|