propro-utils 1.4.95 → 1.4.97
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 +2 -1
- package/src/server/index.js +33 -27
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "propro-utils",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.97",
|
|
4
4
|
"description": "Auth middleware for propro-auth",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"dotenv": "^16.4.1",
|
|
42
42
|
"express": "^4.18.3",
|
|
43
43
|
"express-rate-limit": "^7.1.4",
|
|
44
|
+
"form-data": "^4.0.0",
|
|
44
45
|
"jest": "^29.7.0",
|
|
45
46
|
"jest-mock-axios": "^4.7.3",
|
|
46
47
|
"jsonwebtoken": "^9.0.2",
|
package/src/server/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const { checkIfUserExists } = require('../../middlewares/account_info');
|
|
|
11
11
|
const authValidation = require('../../middlewares/access_token');
|
|
12
12
|
const axios = require('axios');
|
|
13
13
|
const { Router, query } = require('express');
|
|
14
|
+
const FormData = require('form-data');
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Middleware for handling authentication and authorization.
|
|
@@ -71,7 +72,6 @@ class AuthMiddleware {
|
|
|
71
72
|
};
|
|
72
73
|
|
|
73
74
|
handleLogin = async (req, res) => {
|
|
74
|
-
const { email, name, password } = req.body;
|
|
75
75
|
const { returnTokens } = req.query;
|
|
76
76
|
|
|
77
77
|
console.log('returnTokens:', returnTokens);
|
|
@@ -81,15 +81,7 @@ class AuthMiddleware {
|
|
|
81
81
|
const response = await this.proxyToAuthServer(
|
|
82
82
|
req,
|
|
83
83
|
`/api/v1/auth/login?returnTokens=${returnTokens}`,
|
|
84
|
-
|
|
85
|
-
{
|
|
86
|
-
email,
|
|
87
|
-
name,
|
|
88
|
-
password,
|
|
89
|
-
clientId: this.options.clientId,
|
|
90
|
-
redirectUri: this.options.redirectUri,
|
|
91
|
-
returnTokens,
|
|
92
|
-
}
|
|
84
|
+
req.body
|
|
93
85
|
);
|
|
94
86
|
|
|
95
87
|
console.log('response:', JSON.stringify(response.data));
|
|
@@ -110,22 +102,13 @@ class AuthMiddleware {
|
|
|
110
102
|
};
|
|
111
103
|
|
|
112
104
|
handleRegister = async (req, res) => {
|
|
113
|
-
const { name, email, password } = req.body;
|
|
114
105
|
const { returnTokens } = req.query;
|
|
115
106
|
|
|
116
107
|
try {
|
|
117
108
|
const response = await this.proxyToAuthServer(
|
|
118
109
|
req,
|
|
119
110
|
`/api/v1/auth/register?returnTokens=${returnTokens}`,
|
|
120
|
-
|
|
121
|
-
{
|
|
122
|
-
name,
|
|
123
|
-
email,
|
|
124
|
-
password,
|
|
125
|
-
clientId: this.options.clientId,
|
|
126
|
-
redirectUri: this.options.redirectUri,
|
|
127
|
-
returnTokens,
|
|
128
|
-
}
|
|
111
|
+
req.body
|
|
129
112
|
);
|
|
130
113
|
|
|
131
114
|
const { account, tokens } = response.data;
|
|
@@ -255,7 +238,8 @@ class AuthMiddleware {
|
|
|
255
238
|
try {
|
|
256
239
|
const response = await this.proxyToAuthServer(
|
|
257
240
|
req,
|
|
258
|
-
'/api/v1/accounts/profile'
|
|
241
|
+
'/api/v1/accounts/profile',
|
|
242
|
+
req.body
|
|
259
243
|
);
|
|
260
244
|
res.status(response.status).json(response.data);
|
|
261
245
|
} catch (error) {
|
|
@@ -267,7 +251,8 @@ class AuthMiddleware {
|
|
|
267
251
|
try {
|
|
268
252
|
const response = await this.proxyToAuthServer(
|
|
269
253
|
req,
|
|
270
|
-
'/api/v1/profile/password'
|
|
254
|
+
'/api/v1/profile/password',
|
|
255
|
+
req.body
|
|
271
256
|
);
|
|
272
257
|
res.status(response.status).json(response.data);
|
|
273
258
|
} catch (error) {
|
|
@@ -279,7 +264,8 @@ class AuthMiddleware {
|
|
|
279
264
|
try {
|
|
280
265
|
const response = await this.proxyToAuthServer(
|
|
281
266
|
req,
|
|
282
|
-
'/api/v1/profile/email'
|
|
267
|
+
'/api/v1/profile/email',
|
|
268
|
+
req.body
|
|
283
269
|
);
|
|
284
270
|
res.status(response.status).json(response.data);
|
|
285
271
|
} catch (error) {
|
|
@@ -289,7 +275,11 @@ class AuthMiddleware {
|
|
|
289
275
|
|
|
290
276
|
handleTwoFactorAuth = async (req, res) => {
|
|
291
277
|
try {
|
|
292
|
-
const response = await this.proxyToAuthServer(
|
|
278
|
+
const response = await this.proxyToAuthServer(
|
|
279
|
+
req,
|
|
280
|
+
'/api/v1/profile/2fa',
|
|
281
|
+
req.body
|
|
282
|
+
);
|
|
293
283
|
res.status(response.status).json(response.data);
|
|
294
284
|
} catch (error) {
|
|
295
285
|
this.handleProxyError(error, res);
|
|
@@ -300,7 +290,8 @@ class AuthMiddleware {
|
|
|
300
290
|
try {
|
|
301
291
|
const response = await this.proxyToAuthServer(
|
|
302
292
|
req,
|
|
303
|
-
'/api/v1/accounts/avatar'
|
|
293
|
+
'/api/v1/accounts/avatar',
|
|
294
|
+
req.file
|
|
304
295
|
);
|
|
305
296
|
res.status(response.status).json(response.data);
|
|
306
297
|
} catch (error) {
|
|
@@ -315,7 +306,7 @@ class AuthMiddleware {
|
|
|
315
306
|
);
|
|
316
307
|
};
|
|
317
308
|
|
|
318
|
-
proxyToAuthServer = async (req, path) => {
|
|
309
|
+
proxyToAuthServer = async (req, path, body) => {
|
|
319
310
|
let accessToken = null;
|
|
320
311
|
if (this.pathRequiresAccessToken(path)) {
|
|
321
312
|
console.log('path requires access token');
|
|
@@ -339,13 +330,28 @@ class AuthMiddleware {
|
|
|
339
330
|
formattedAuthUrl = `${formattedAuthUrl}&clientId=${this.options.clientId}&redirectUri=${this.options.redirectUri}`;
|
|
340
331
|
}
|
|
341
332
|
|
|
333
|
+
const formData = new FormData();
|
|
334
|
+
if (req.file) {
|
|
335
|
+
formData.append('file', req.file.buffer, {
|
|
336
|
+
filename: req.file.originalname,
|
|
337
|
+
contentType: req.file.mimetype,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
342
341
|
console.log('formattedAuthUrl:', formattedAuthUrl);
|
|
343
342
|
console.log('req.method:', req.method);
|
|
343
|
+
console.log('req.body:', req.body);
|
|
344
|
+
console.log('req.file:', req.file);
|
|
345
|
+
console.log('formData:', JSON.stringify(formData));
|
|
346
|
+
console.log('headers:', JSON.stringify(headers));
|
|
344
347
|
return axios({
|
|
345
348
|
method: req.method,
|
|
346
349
|
url: `${formattedAuthUrl}${path}`,
|
|
347
350
|
data: req.body,
|
|
348
|
-
headers:
|
|
351
|
+
headers: {
|
|
352
|
+
...headers,
|
|
353
|
+
...formData.getHeaders(),
|
|
354
|
+
},
|
|
349
355
|
});
|
|
350
356
|
};
|
|
351
357
|
|