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,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.5.22",
3
+ "version": "1.5.24",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -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
- let domain = appUrl ? new URL(appUrl).hostname : undefined;
30
-
31
- if (domain.includes('mapmap.app')) {
32
- domain = '.mapmap.app';
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
- // secure: process.env.NODE_ENV === 'production',
37
- secure: true,
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
- res.cookie('x-refresh-token', tokens.refresh.token, {
44
- httpOnly: true,
45
- maxAge: refreshMaxAge,
46
- ...commonAttributes,
47
- });
53
+ if (commonAttributes.sameSite === 'None') {
54
+ commonAttributes.secure = true;
55
+ }
48
56
 
49
- res.cookie('x-access-token', tokens.access.token, {
50
- httpOnly: true,
51
- maxAge: accessMaxAge,
52
- ...commonAttributes,
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
- res.cookie('user', JSON.stringify(user), {
56
- maxAge: refreshMaxAge,
57
- ...commonAttributes,
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
- res.cookie('account', JSON.stringify(account), {
61
- maxAge: refreshMaxAge,
62
- ...commonAttributes,
63
- });
78
+ const sanitizedUser = { ...user };
79
+ delete sanitizedUser.password;
64
80
 
65
- res.cookie(
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
- const clearAuthCookies = res => {
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: true,
78
- sameSite: 'None',
79
- domain: process.env.APP_URL
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
- ].forEach(cookieName => {
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 = {