propro-utils 1.6.9 → 1.7.1

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,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.6.9",
3
+ "version": "1.7.1",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -99,7 +99,6 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
99
99
  new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
100
100
 
101
101
  // Domain configuration
102
- let domain;
103
102
  let isSecureConnection = false;
104
103
  try {
105
104
  // Handle URLs that don't include the protocol
@@ -113,23 +112,9 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
113
112
  }
114
113
 
115
114
  const urlObj = new URL(processedAppUrl);
116
- domain = urlObj.hostname;
117
115
  isSecureConnection = urlObj.protocol === "https:";
118
116
 
119
- if (domain?.includes("mapmap.app")) {
120
- domain = ".mapmap.app";
121
- }
122
- if (domain?.includes("localhost")) {
123
- domain = undefined;
124
- isSecureConnection = false;
125
- }
126
- if (domain?.includes("propro.so")) {
127
- // Always set cookies on the root domain for all propro.so subdomains
128
- domain = ".propro.so";
129
- }
130
-
131
117
  console.log("Cookie configuration:", {
132
- domain,
133
118
  isSecure: isSecureConnection,
134
119
  protocol: urlObj.protocol,
135
120
  originalUrl: appUrl,
@@ -138,37 +123,26 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
138
123
  });
139
124
  } catch (error) {
140
125
  console.error("Invalid appUrl:", { error, appUrl });
141
- domain = undefined;
142
126
  isSecureConnection = false;
143
127
  }
144
128
 
145
- // Determine if we're in a local development environment
146
- const isLocalhost =
147
- !domain || domain === "localhost" || domain.includes("localhost");
148
-
129
+ // Base cookie attributes without domain specification
149
130
  const commonAttributes = {
150
- secure: isSecureConnection,
151
- sameSite: isSecureConnection ? "None" : "Lax",
152
- domain,
131
+ secure: true,
132
+ sameSite: "None",
153
133
  path: "/",
154
- httpOnly: false,
155
- expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1 year expiry as fallback
156
134
  };
157
135
 
158
- console.log("Cookie attributes:", commonAttributes);
159
-
160
136
  const httpOnlyCookies = {
161
137
  "x-refresh-token": {
162
138
  value: tokens.refresh.token,
163
139
  maxAge: refreshMaxAge,
164
140
  httpOnly: true,
165
- secure: isSecureConnection,
166
141
  },
167
142
  "x-access-token": {
168
143
  value: tokens.access.token,
169
144
  maxAge: accessMaxAge,
170
145
  httpOnly: true,
171
- secure: isSecureConnection,
172
146
  },
173
147
  };
174
148
 
@@ -181,53 +155,58 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
181
155
  value: safeStringify(sanitizedUser),
182
156
  maxAge: refreshMaxAge,
183
157
  httpOnly: false,
184
- secure: isSecureConnection,
185
158
  },
186
159
  account: {
187
160
  value: safeStringify(sanitizedAccount),
188
161
  maxAge: refreshMaxAge,
189
162
  httpOnly: false,
190
- secure: isSecureConnection,
191
163
  },
192
164
  has_account_token: {
193
165
  value: JSON.stringify({ value: "true", expires: accessMaxAge }),
194
166
  maxAge: accessMaxAge,
195
167
  httpOnly: false,
196
- secure: isSecureConnection,
197
168
  },
198
169
  };
199
170
 
200
171
  try {
172
+ // Set each cookie individually
201
173
  Object.entries({ ...httpOnlyCookies, ...regularCookies }).forEach(
202
174
  ([name, config]) => {
203
- res.cookie(name, config.value, {
175
+ const cookieConfig = {
204
176
  ...commonAttributes,
205
177
  ...config,
206
- });
178
+ };
179
+
180
+ res.cookie(name, config.value, cookieConfig);
207
181
  }
208
182
  );
209
183
 
210
- const extensionCookiePromises = Object.entries({
211
- ...httpOnlyCookies,
212
- ...regularCookies,
213
- }).map(([name, config]) => {
214
- return setChromeExtensionCookie({
215
- url: `https://${domain || "propro.so"}`,
216
- name,
217
- value: config.value,
218
- secure: true,
219
- httpOnly: !!config.httpOnly,
220
- sameSite: "no_restriction",
221
- path: "/",
222
- expirationDate: Math.floor((Date.now() + config.maxAge) / 1000),
223
- domain: domain?.startsWith(".") ? domain : `.${domain || "propro.so"}`,
184
+ // Handle extension cookies if in extension context
185
+ try {
186
+ const extensionCookiePromises = Object.entries({
187
+ ...httpOnlyCookies,
188
+ ...regularCookies,
189
+ }).map(([name, config]) => {
190
+ return setChromeExtensionCookie({
191
+ url: processedAppUrl,
192
+ name,
193
+ value: config.value,
194
+ secure: true,
195
+ httpOnly: !!config.httpOnly,
196
+ sameSite: "no_restriction",
197
+ path: "/",
198
+ expirationDate: Math.floor((Date.now() + config.maxAge) / 1000),
199
+ });
224
200
  });
225
- });
226
201
 
227
- await Promise.allSettled(extensionCookiePromises);
202
+ Promise.allSettled(extensionCookiePromises).catch(() => {
203
+ // Ignore extension errors
204
+ });
205
+ } catch (error) {
206
+ // Ignore extension errors
207
+ }
228
208
 
229
209
  console.log("Auth cookies set successfully", {
230
- domain,
231
210
  sameSite: commonAttributes.sameSite,
232
211
  cookieNames: [
233
212
  ...Object.keys(httpOnlyCookies),
@@ -247,7 +226,6 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
247
226
  * Clears cookies from both web and extension contexts
248
227
  */
249
228
  const clearAuthCookies = async (res, appUrl) => {
250
- let domain;
251
229
  let isSecureConnection = false;
252
230
  try {
253
231
  // Handle URLs that don't include the protocol
@@ -261,23 +239,9 @@ const clearAuthCookies = async (res, appUrl) => {
261
239
  }
262
240
 
263
241
  const urlObj = new URL(processedAppUrl);
264
- domain = urlObj.hostname;
265
242
  isSecureConnection = urlObj.protocol === "https:";
266
243
 
267
- if (domain?.includes("mapmap.app")) {
268
- domain = ".mapmap.app";
269
- }
270
- if (domain?.includes("localhost")) {
271
- domain = undefined;
272
- isSecureConnection = false;
273
- }
274
- if (domain?.includes("propro.so")) {
275
- // Always set cookies on the root domain for all propro.so subdomains
276
- domain = ".propro.so";
277
- }
278
-
279
244
  console.log("Clear cookies configuration:", {
280
- domain,
281
245
  isSecure: isSecureConnection,
282
246
  protocol: urlObj.protocol,
283
247
  originalUrl: appUrl,
@@ -286,21 +250,16 @@ const clearAuthCookies = async (res, appUrl) => {
286
250
  });
287
251
  } catch (error) {
288
252
  console.error("Invalid appUrl:", error);
289
- domain = undefined;
290
253
  isSecureConnection = false;
291
254
  }
292
255
 
293
256
  const commonAttributes = {
294
- secure: isSecureConnection,
295
- sameSite: isSecureConnection ? "None" : "Lax",
296
- domain,
257
+ secure: true,
258
+ sameSite: "None",
297
259
  path: "/",
298
- httpOnly: false,
299
- expires: new Date(0), // Set expiry to past date to ensure cookie is removed
260
+ expires: new Date(0),
300
261
  };
301
262
 
302
- console.log("Clear cookie attributes:", commonAttributes);
303
-
304
263
  const cookieNames = [
305
264
  "x-refresh-token",
306
265
  "x-access-token",
@@ -309,32 +268,35 @@ const clearAuthCookies = async (res, appUrl) => {
309
268
  "has_account_token",
310
269
  ];
311
270
 
312
- // Clear web cookies
271
+ // Clear cookies with domain
313
272
  cookieNames.forEach((cookieName) => {
314
273
  res.clearCookie(cookieName, commonAttributes);
315
274
  });
316
275
 
317
276
  try {
277
+ // Handle extension cookies if in extension context
318
278
  const extensionClearPromises = cookieNames.map(
319
279
  (name) =>
320
280
  new Promise((resolve) => {
321
281
  chrome.cookies.remove(
322
282
  {
323
- url: `https://${domain || "mapmap.app"}`,
283
+ url: processedAppUrl,
324
284
  name,
285
+ secure: true,
325
286
  },
326
287
  resolve
327
288
  );
328
289
  })
329
290
  );
330
291
 
331
- await Promise.allSettled(extensionClearPromises);
292
+ Promise.allSettled(extensionClearPromises).catch(() => {
293
+ // Ignore extension errors
294
+ });
332
295
  } catch (error) {
333
296
  // Not in extension context, ignore
334
297
  }
335
298
 
336
299
  console.log("Auth cookies cleared successfully", {
337
- domain,
338
300
  cookieNames,
339
301
  sameSite: commonAttributes.sameSite,
340
302
  });