propro-utils 1.5.22 → 1.5.24
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
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication Cookies Module
|
|
3
|
+
* This module provides functions for setting and clearing authentication cookies.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { URL } = require('url');
|
|
7
|
+
|
|
1
8
|
/**
|
|
2
9
|
* Sets the authentication cookies in the response object.
|
|
3
10
|
*
|
|
@@ -11,85 +18,134 @@ const setAuthCookies = (res, tokens, account, user, appUrl) => {
|
|
|
11
18
|
if (!tokens || !tokens.refresh || !tokens.access) {
|
|
12
19
|
throw new Error('Invalid tokens object');
|
|
13
20
|
}
|
|
14
|
-
|
|
15
21
|
if (!account) {
|
|
16
22
|
throw new Error('Invalid account object');
|
|
17
23
|
}
|
|
18
|
-
|
|
19
24
|
if (!user) {
|
|
20
25
|
throw new Error('Invalid user object');
|
|
21
26
|
}
|
|
22
|
-
const currentDateTime = new Date();
|
|
23
27
|
|
|
28
|
+
const currentDateTime = new Date();
|
|
24
29
|
const refreshMaxAge =
|
|
25
30
|
new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
|
|
26
31
|
const accessMaxAge =
|
|
27
32
|
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
28
33
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
domain =
|
|
34
|
+
// Determine the domain
|
|
35
|
+
let domain;
|
|
36
|
+
try {
|
|
37
|
+
domain = appUrl ? new URL(appUrl).hostname : undefined;
|
|
38
|
+
if (domain && domain.includes('mapmap.app')) {
|
|
39
|
+
domain = '.mapmap.app';
|
|
40
|
+
}
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error('Invalid appUrl:', error);
|
|
43
|
+
domain = undefined;
|
|
33
44
|
}
|
|
34
45
|
|
|
46
|
+
// Define common cookie attributes
|
|
35
47
|
const commonAttributes = {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
sameSite: 'None',
|
|
39
|
-
// path: '/',
|
|
48
|
+
secure: process.env.NODE_ENV === 'production',
|
|
49
|
+
sameSite: process.env.NODE_ENV === 'production' ? 'None' : 'Lax',
|
|
40
50
|
domain: domain,
|
|
41
51
|
};
|
|
42
52
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
...commonAttributes,
|
|
47
|
-
});
|
|
53
|
+
if (commonAttributes.sameSite === 'None') {
|
|
54
|
+
commonAttributes.secure = true;
|
|
55
|
+
}
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
const httpOnlyCookies = [
|
|
58
|
+
{
|
|
59
|
+
name: 'x-refresh-token',
|
|
60
|
+
value: tokens.refresh.token,
|
|
61
|
+
maxAge: refreshMaxAge,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'x-access-token',
|
|
65
|
+
value: tokens.access.token,
|
|
66
|
+
maxAge: accessMaxAge,
|
|
67
|
+
},
|
|
68
|
+
];
|
|
54
69
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
70
|
+
httpOnlyCookies.forEach(cookie => {
|
|
71
|
+
res.cookie(cookie.name, cookie.value, {
|
|
72
|
+
...commonAttributes,
|
|
73
|
+
httpOnly: true,
|
|
74
|
+
maxAge: cookie.maxAge,
|
|
75
|
+
});
|
|
58
76
|
});
|
|
59
77
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
...commonAttributes,
|
|
63
|
-
});
|
|
78
|
+
const sanitizedUser = { ...user };
|
|
79
|
+
delete sanitizedUser.password;
|
|
64
80
|
|
|
65
|
-
|
|
66
|
-
'has_account_token',
|
|
67
|
-
JSON.stringify({ value: 'true', expires: accessMaxAge }),
|
|
81
|
+
const regularCookies = [
|
|
68
82
|
{
|
|
83
|
+
name: 'user',
|
|
84
|
+
value: JSON.stringify(sanitizedUser),
|
|
85
|
+
maxAge: refreshMaxAge,
|
|
86
|
+
},
|
|
87
|
+
{ name: 'account', value: JSON.stringify(account), maxAge: refreshMaxAge },
|
|
88
|
+
{
|
|
89
|
+
name: 'has_account_token',
|
|
90
|
+
value: JSON.stringify({ value: 'true', expires: accessMaxAge }),
|
|
69
91
|
maxAge: accessMaxAge,
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
regularCookies.forEach(cookie => {
|
|
96
|
+
res.cookie(cookie.name, cookie.value, {
|
|
70
97
|
...commonAttributes,
|
|
71
|
-
|
|
72
|
-
|
|
98
|
+
maxAge: cookie.maxAge,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
console.log('Setting auth cookies');
|
|
103
|
+
console.log('tokens', tokens);
|
|
104
|
+
console.log('account', account);
|
|
105
|
+
console.log('sanitizedUser', sanitizedUser);
|
|
73
106
|
};
|
|
74
107
|
|
|
75
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Clears all authentication cookies from the response object.
|
|
110
|
+
*
|
|
111
|
+
* @param {Object} res - The response object.
|
|
112
|
+
* @param {string} appUrl - The URL of the client application.
|
|
113
|
+
*/
|
|
114
|
+
const clearAuthCookies = (res, appUrl) => {
|
|
115
|
+
let domain;
|
|
116
|
+
try {
|
|
117
|
+
domain = appUrl ? new URL(appUrl).hostname : undefined;
|
|
118
|
+
if (domain && domain.includes('mapmap.app')) {
|
|
119
|
+
domain = '.mapmap.app';
|
|
120
|
+
}
|
|
121
|
+
} catch (error) {
|
|
122
|
+
console.error('Invalid appUrl:', error);
|
|
123
|
+
domain = undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
76
126
|
const commonAttributes = {
|
|
77
|
-
secure:
|
|
78
|
-
sameSite: 'None',
|
|
79
|
-
domain:
|
|
80
|
-
? new URL(process.env.APP_URL).hostname
|
|
81
|
-
: undefined,
|
|
127
|
+
secure: process.env.NODE_ENV === 'production',
|
|
128
|
+
sameSite: process.env.NODE_ENV === 'production' ? 'None' : 'Lax',
|
|
129
|
+
domain: domain,
|
|
82
130
|
};
|
|
83
131
|
|
|
84
|
-
|
|
132
|
+
if (commonAttributes.sameSite === 'None') {
|
|
133
|
+
commonAttributes.secure = true;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const cookieNames = [
|
|
85
137
|
'x-refresh-token',
|
|
86
138
|
'x-access-token',
|
|
87
139
|
'user',
|
|
88
140
|
'account',
|
|
89
141
|
'has_account_token',
|
|
90
|
-
]
|
|
142
|
+
];
|
|
143
|
+
|
|
144
|
+
cookieNames.forEach(cookieName => {
|
|
91
145
|
res.clearCookie(cookieName, commonAttributes);
|
|
92
146
|
});
|
|
147
|
+
|
|
148
|
+
console.log('Cleared auth cookies');
|
|
93
149
|
};
|
|
94
150
|
|
|
95
151
|
module.exports = {
|