propro-utils 1.3.30 → 1.3.32
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 +2 -5
- package/package.json +1 -1
- package/src/index.js +3 -2
- package/src/server/index.js +5 -3
|
@@ -60,12 +60,9 @@ const getAccountProfile = async (redisClient, userSchema, accountId) => {
|
|
|
60
60
|
* @returns {Promise<void>} - A promise that resolves once the check/create operation is done.
|
|
61
61
|
*/
|
|
62
62
|
const checkIfUserExists = async (userSchema, accountId) => {
|
|
63
|
-
const user = await userSchema.findOne({ accountId
|
|
64
|
-
});
|
|
63
|
+
const user = await userSchema.findOne({ accountId });
|
|
65
64
|
if (!user) {
|
|
66
|
-
await userSchema.create({
|
|
67
|
-
accountId: accountId
|
|
68
|
-
});
|
|
65
|
+
await userSchema.create({accountId});
|
|
69
66
|
}
|
|
70
67
|
};
|
|
71
68
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -24,6 +24,7 @@ let _serverAuth, _clientAuth;
|
|
|
24
24
|
* }
|
|
25
25
|
* @param {boolean} [options.useClientAuth=false] - A boolean flag to enable client-side authentication.
|
|
26
26
|
* @param {Object} [options.clientOptions={}] - Configuration options for client-side authentication.
|
|
27
|
+
* @param {Schema} [userSchema] - The user schema to perform the operations on.
|
|
27
28
|
*
|
|
28
29
|
* @returns {Function} An Express middleware function.
|
|
29
30
|
*
|
|
@@ -40,7 +41,7 @@ let _serverAuth, _clientAuth;
|
|
|
40
41
|
* useClientAuth: false,
|
|
41
42
|
* }));
|
|
42
43
|
*/
|
|
43
|
-
module.exports = function proproAuthMiddleware(options = {}) {
|
|
44
|
+
module.exports = function proproAuthMiddleware(options = {}, userSchema) {
|
|
44
45
|
validateEnvironmentVariables([
|
|
45
46
|
"AUTH_URL",
|
|
46
47
|
"CLIENT_ID",
|
|
@@ -52,7 +53,7 @@ module.exports = function proproAuthMiddleware(options = {}) {
|
|
|
52
53
|
try {
|
|
53
54
|
// Lazy loading and initializing server and client authentication modules with options
|
|
54
55
|
if (options.useServerAuth) {
|
|
55
|
-
_serverAuth = _serverAuth || require("./server")(options.serverOptions);
|
|
56
|
+
_serverAuth = _serverAuth || require("./server")(options.serverOptions, userSchema);
|
|
56
57
|
_serverAuth(req, res, next);
|
|
57
58
|
} else if (options.useClientAuth) {
|
|
58
59
|
_clientAuth = _clientAuth || require("./client")(options.clientOptions);
|
package/src/server/index.js
CHANGED
|
@@ -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,8 +49,6 @@ function proproAuthMiddleware(options = {}) {
|
|
|
47
49
|
return res.status(400).send("No code received");
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
console.log("code", code);
|
|
51
|
-
|
|
52
52
|
const {tokens, account, redirectUrl} = await exchangeToken(
|
|
53
53
|
authUrl,
|
|
54
54
|
code,
|
|
@@ -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();
|