propro-utils 1.4.37 → 1.4.38
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/server/index.js +40 -53
package/package.json
CHANGED
package/src/server/index.js
CHANGED
|
@@ -33,6 +33,43 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
33
33
|
|
|
34
34
|
let refreshToken;
|
|
35
35
|
|
|
36
|
+
const setAuthCookies = (res, tokens, account, user) => {
|
|
37
|
+
const currentDateTime = new Date();
|
|
38
|
+
|
|
39
|
+
const refreshMaxAge =
|
|
40
|
+
new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
|
|
41
|
+
|
|
42
|
+
res.cookie('x-refresh-token', tokens.refresh.token, {
|
|
43
|
+
httpOnly: true,
|
|
44
|
+
secure: process.env.NODE_ENV === 'production',
|
|
45
|
+
maxAge: refreshMaxAge,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const accessMaxAge =
|
|
49
|
+
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
50
|
+
|
|
51
|
+
res.cookie('x-access-token', tokens.access.token, {
|
|
52
|
+
httpOnly: true,
|
|
53
|
+
secure: process.env.NODE_ENV === 'production',
|
|
54
|
+
maxAge: accessMaxAge,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
res.cookie('user', JSON.stringify(user), {
|
|
58
|
+
maxAge: refreshMaxAge,
|
|
59
|
+
secure: process.env.NODE_ENV === 'production',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
res.cookie('account', JSON.stringify(account), {
|
|
63
|
+
maxAge: refreshMaxAge,
|
|
64
|
+
secure: process.env.NODE_ENV === 'production',
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
res.cookie('has_account_token', 'true', {
|
|
68
|
+
maxAge: accessMaxAge,
|
|
69
|
+
secure: process.env.NODE_ENV === 'production',
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
36
73
|
return async (req, res, next) => {
|
|
37
74
|
try {
|
|
38
75
|
if (
|
|
@@ -81,9 +118,6 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
81
118
|
}
|
|
82
119
|
);
|
|
83
120
|
|
|
84
|
-
const accessMaxAge =
|
|
85
|
-
new Date(access.expires).getTime() - currentDateTime.getTime();
|
|
86
|
-
|
|
87
121
|
const { account, access, refresh } = response.data;
|
|
88
122
|
|
|
89
123
|
if (!account || !access || !refresh) {
|
|
@@ -92,22 +126,9 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
92
126
|
.json({ error: 'Invalid or expired refresh token' });
|
|
93
127
|
}
|
|
94
128
|
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
const refreshMaxAge =
|
|
98
|
-
new Date(refresh.expires).getTime() - currentDateTime.getTime();
|
|
99
|
-
|
|
100
|
-
res.cookie('x-refresh-token', refresh.token, {
|
|
101
|
-
httpOnly: true,
|
|
102
|
-
secure: process.env.NODE_ENV === 'production',
|
|
103
|
-
maxAge: refreshMaxAge,
|
|
104
|
-
});
|
|
129
|
+
const user = await checkIfUserExists(userSchema, account.accountId);
|
|
105
130
|
|
|
106
|
-
res
|
|
107
|
-
httpOnly: true,
|
|
108
|
-
secure: process.env.NODE_ENV === 'production',
|
|
109
|
-
maxAge: accessMaxAge,
|
|
110
|
-
});
|
|
131
|
+
setAuthCookies(res, { access, refresh }, account, user);
|
|
111
132
|
|
|
112
133
|
return res
|
|
113
134
|
.status(200)
|
|
@@ -130,41 +151,7 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
130
151
|
|
|
131
152
|
const user = await checkIfUserExists(userSchema, account.accountId);
|
|
132
153
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const refreshMaxAge =
|
|
136
|
-
new Date(tokens.refresh.expires).getTime() -
|
|
137
|
-
currentDateTime.getTime();
|
|
138
|
-
|
|
139
|
-
res.cookie('x-refresh-token', tokens.refresh.token, {
|
|
140
|
-
httpOnly: true,
|
|
141
|
-
secure: process.env.NODE_ENV === 'production',
|
|
142
|
-
maxAge: refreshMaxAge,
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const accessMaxAge =
|
|
146
|
-
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
147
|
-
|
|
148
|
-
res.cookie('x-access-token', tokens.access.token, {
|
|
149
|
-
httpOnly: true,
|
|
150
|
-
secure: process.env.NODE_ENV === 'production',
|
|
151
|
-
maxAge: accessMaxAge,
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
res.cookie('user', JSON.stringify(user), {
|
|
155
|
-
maxAge: refreshMaxAge,
|
|
156
|
-
secure: process.env.NODE_ENV === 'production',
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
res.cookie('account', JSON.stringify(account), {
|
|
160
|
-
maxAge: refreshMaxAge,
|
|
161
|
-
secure: process.env.NODE_ENV === 'production',
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
res.cookie('has_account_token', 'true', {
|
|
165
|
-
maxAge: accessMaxAge,
|
|
166
|
-
secure: process.env.NODE_ENV === 'production',
|
|
167
|
-
});
|
|
154
|
+
setAuthCookies(res, tokens, account, user);
|
|
168
155
|
|
|
169
156
|
const urlToRedirect = formatRedirectUrl(redirectUrl);
|
|
170
157
|
|