propro-utils 1.4.52 → 1.4.53
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/package.json +1 -1
- package/src/index.js +14 -11
- package/src/server/index.js +3 -1
- package/src/server/middleware/setAuthCookies.js +3 -3
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const {
|
|
2
2
|
validateEnvironmentVariables,
|
|
3
|
-
} = require(
|
|
3
|
+
} = require('./server/middleware/validateEnv');
|
|
4
4
|
let _serverAuth, _clientAuth;
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -9,7 +9,7 @@ let _serverAuth, _clientAuth;
|
|
|
9
9
|
* @param {Object} options - Configuration options for the middleware.
|
|
10
10
|
* @param {boolean} [options.useServerAuth=true] - A boolean flag to enable server-side authentication.
|
|
11
11
|
* @param {Object} [options.serverOptions={}] - Configuration options for server-side authentication.
|
|
12
|
-
* Example:
|
|
12
|
+
* ```Example:
|
|
13
13
|
* {
|
|
14
14
|
* jwtSecret: 'HubHubJWTSecret', // Secret key for JWT token verification
|
|
15
15
|
* tokenExpiry: 3600, // Token expiry time in seconds
|
|
@@ -22,12 +22,13 @@ let _serverAuth, _clientAuth;
|
|
|
22
22
|
* onAuthFailRedirect: '/login', // URL to redirect on authentication failure
|
|
23
23
|
* additionalChecks: async (req) => { }, // Additional custom checks for requests
|
|
24
24
|
* },
|
|
25
|
+
* ```
|
|
25
26
|
* @param {boolean} [options.useClientAuth=false] - A boolean flag to enable client-side authentication.
|
|
26
27
|
* @param {Object} [options.clientOptions={}] - Configuration options for client-side authentication.
|
|
27
28
|
* @param {Schema} [userSchema] - The user schema to perform the operations on.
|
|
28
29
|
*
|
|
29
30
|
* @returns {Function} An Express middleware function.
|
|
30
|
-
*
|
|
31
|
+
* ```
|
|
31
32
|
* Example usage:
|
|
32
33
|
* app.use(proproAuthMiddleware({
|
|
33
34
|
* useServerAuth: true,
|
|
@@ -40,29 +41,31 @@ let _serverAuth, _clientAuth;
|
|
|
40
41
|
* },
|
|
41
42
|
* useClientAuth: false,
|
|
42
43
|
* }, UserSchema));
|
|
44
|
+
* ```
|
|
43
45
|
*/
|
|
44
46
|
module.exports = function proproAuthMiddleware(options = {}, userSchema) {
|
|
45
47
|
validateEnvironmentVariables([
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
'AUTH_URL',
|
|
49
|
+
'CLIENT_ID',
|
|
50
|
+
'CLIENT_SECRET',
|
|
51
|
+
'CLIENT_URL',
|
|
52
|
+
'REDIRECT_URI',
|
|
51
53
|
]);
|
|
52
54
|
return (req, res, next) => {
|
|
53
55
|
try {
|
|
54
56
|
// Lazy loading and initializing server and client authentication modules with options
|
|
55
57
|
if (options.useServerAuth) {
|
|
56
|
-
_serverAuth =
|
|
58
|
+
_serverAuth =
|
|
59
|
+
_serverAuth || require('./server')(options.serverOptions, userSchema);
|
|
57
60
|
_serverAuth(req, res, next);
|
|
58
61
|
} else if (options.useClientAuth) {
|
|
59
|
-
_clientAuth = _clientAuth || require(
|
|
62
|
+
_clientAuth = _clientAuth || require('./client')(options.clientOptions);
|
|
60
63
|
_clientAuth(req, res, next);
|
|
61
64
|
} else {
|
|
62
65
|
next();
|
|
63
66
|
}
|
|
64
67
|
} catch (error) {
|
|
65
|
-
console.error(
|
|
68
|
+
console.error('Error in authentication middleware:', error);
|
|
66
69
|
next(error);
|
|
67
70
|
}
|
|
68
71
|
};
|
package/src/server/index.js
CHANGED
|
@@ -18,6 +18,7 @@ const { post } = require('axios');
|
|
|
18
18
|
* @param {string} [options.clientUrl=process.env.CLIENT_URL] - The client URL.
|
|
19
19
|
* @param {string} [options.redirectUri=process.env.REDIRECT_URI] - The redirect URI.
|
|
20
20
|
* @param {string} [options.appName=process.env.APP_NAME] - The application name.
|
|
21
|
+
* @param {string} [options.appUrl] - The URL of the client application.
|
|
21
22
|
* @param {Schema} [userSchema] - The user schema to perform the operations on.
|
|
22
23
|
* @returns {Function} - Express middleware function.
|
|
23
24
|
*/
|
|
@@ -30,6 +31,7 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
30
31
|
clientUrl = process.env.CLIENT_URL,
|
|
31
32
|
redirectUri = process.env.REDIRECT_URI,
|
|
32
33
|
appName = process.env.APP_NAME,
|
|
34
|
+
appUrl = process.env.APP_URL,
|
|
33
35
|
} = options;
|
|
34
36
|
|
|
35
37
|
let refreshToken;
|
|
@@ -121,7 +123,7 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
121
123
|
|
|
122
124
|
const user = await checkIfUserExists(userSchema, account.accountId);
|
|
123
125
|
|
|
124
|
-
setAuthCookies(res, tokens, account, user,
|
|
126
|
+
setAuthCookies(res, tokens, account, user, appUrl);
|
|
125
127
|
|
|
126
128
|
const urlToRedirect = formatRedirectUrl(redirectUrl);
|
|
127
129
|
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
* @param {Object} tokens - The authentication tokens.
|
|
6
6
|
* @param {Object} account - The user's account information.
|
|
7
7
|
* @param {Object} user - The user's information.
|
|
8
|
-
* @param {string}
|
|
8
|
+
* @param {string} appUrl - The URL of the client application.
|
|
9
9
|
*/
|
|
10
|
-
const setAuthCookies = (res, tokens, account, user,
|
|
10
|
+
const setAuthCookies = (res, tokens, account, user, appUrl) => {
|
|
11
11
|
const currentDateTime = new Date();
|
|
12
12
|
|
|
13
13
|
const refreshMaxAge =
|
|
@@ -19,7 +19,7 @@ const setAuthCookies = (res, tokens, account, user, redirectUri) => {
|
|
|
19
19
|
secure: process.env.NODE_ENV === 'production',
|
|
20
20
|
// sameSite: 'Strict',
|
|
21
21
|
// path: '/',
|
|
22
|
-
domain: redirectUri ? new URL(
|
|
22
|
+
domain: redirectUri ? new URL(appUrl).hostname : undefined,
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
res.cookie('x-refresh-token', tokens.refresh.token, {
|