propro-utils 1.3.29 → 1.3.31

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.
@@ -40,14 +40,6 @@ const getAccountProfile = async (redisClient, userSchema, accountId) => {
40
40
  throw new Error("Invalid permissions");
41
41
  }
42
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
43
  return profileData;
52
44
 
53
45
  } catch (error) {
@@ -58,4 +50,23 @@ const getAccountProfile = async (redisClient, userSchema, accountId) => {
58
50
  }
59
51
  };
60
52
 
61
- module.exports = getAccountProfile;
53
+
54
+ /**
55
+ * Checks if a user exists based on the given account ID.
56
+ * If the user does not exist, creates a new user with the given account ID.
57
+ *
58
+ * @param {Schema} userSchema - The user schema to perform the operations on.
59
+ * @param {string} accountId - The account ID of the user to check/create.
60
+ * @returns {Promise<void>} - A promise that resolves once the check/create operation is done.
61
+ */
62
+ const checkIfUserExists = async (userSchema, accountId) => {
63
+ const user = await userSchema.findOne({ accountId });
64
+ if (!user) {
65
+ await userSchema.create({accountId});
66
+ }
67
+ };
68
+
69
+ module.exports = {
70
+ getAccountProfile,
71
+ checkIfUserExists
72
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.3.29",
3
+ "version": "1.3.31",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -2,6 +2,7 @@ require("dotenv").config();
2
2
  const {
3
3
  exchangeToken,
4
4
  } = require("./middleware/verifyToken");
5
+ const {checkIfUserExists} = require("../../middlewares/account_info");
5
6
 
6
7
  /**
7
8
  * Middleware for handling authentication and authorization.
@@ -14,9 +15,10 @@ const {
14
15
  * @param {string} [options.clientUrl=process.env.CLIENT_URL] - The client URL.
15
16
  * @param {string} [options.redirectUri=process.env.REDIRECT_URI] - The redirect URI.
16
17
  * @param {string} [options.appName=process.env.APP_NAME] - The application name.
18
+ * @param {Schema} [userSchema] - The user schema to perform the operations on.
17
19
  * @returns {Function} - Express middleware function.
18
20
  */
19
- function proproAuthMiddleware(options = {}) {
21
+ function proproAuthMiddleware(options = {}, userSchema) {
20
22
  const {
21
23
  secret = "RESTFULAPIs",
22
24
  authUrl = process.env.AUTH_URL,
@@ -47,9 +49,7 @@ function proproAuthMiddleware(options = {}) {
47
49
  return res.status(400).send("No code received");
48
50
  }
49
51
 
50
- console.log("code", code);
51
-
52
- const {tokens, redirectUrl} = await exchangeToken(
52
+ const {tokens, account, redirectUrl} = await exchangeToken(
53
53
  authUrl,
54
54
  code,
55
55
  clientId,
@@ -57,6 +57,8 @@ function proproAuthMiddleware(options = {}) {
57
57
  redirectUri
58
58
  );
59
59
 
60
+ await checkIfUserExists(userSchema, account.accountId);
61
+
60
62
  const currentDateTime = new Date();
61
63
 
62
64
  const refreshMaxAge = new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();