propro-utils 1.4.97 → 1.4.99

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.
Files changed (2) hide show
  1. package/package.json +2 -1
  2. package/src/server/index.js +45 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.4.97",
3
+ "version": "1.4.99",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -45,6 +45,7 @@
45
45
  "jest": "^29.7.0",
46
46
  "jest-mock-axios": "^4.7.3",
47
47
  "jsonwebtoken": "^9.0.2",
48
+ "multer": "^1.4.5-lts.1",
48
49
  "nodemailer": "^6.9.7",
49
50
  "nodemailer-mailgun-transport": "^2.1.5",
50
51
  "querystring": "^0.2.1",
@@ -12,6 +12,8 @@ const authValidation = require('../../middlewares/access_token');
12
12
  const axios = require('axios');
13
13
  const { Router, query } = require('express');
14
14
  const FormData = require('form-data');
15
+ const multer = require('multer');
16
+ const upload = multer().single('file');
15
17
 
16
18
  /**
17
19
  * Middleware for handling authentication and authorization.
@@ -287,16 +289,29 @@ class AuthMiddleware {
287
289
  };
288
290
 
289
291
  handleAvatarUpdate = async (req, res) => {
290
- try {
291
- const response = await this.proxyToAuthServer(
292
- req,
293
- '/api/v1/accounts/avatar',
294
- req.file
295
- );
296
- res.status(response.status).json(response.data);
297
- } catch (error) {
298
- this.handleProxyError(error, res);
299
- }
292
+ upload(req, res, async err => {
293
+ if (err instanceof multer.MulterError) {
294
+ return res.status(400).json({ error: 'File upload error' });
295
+ } else if (err) {
296
+ return res
297
+ .status(500)
298
+ .json({ error: 'Server error during file upload' });
299
+ }
300
+
301
+ if (!req.file) {
302
+ return res.status(400).json({ error: 'No file uploaded' });
303
+ }
304
+
305
+ try {
306
+ const response = await this.proxyToAuthServer(
307
+ req,
308
+ '/api/v1/accounts/avatar'
309
+ );
310
+ res.status(response.status).json(response.data);
311
+ } catch (error) {
312
+ this.handleProxyError(error, res);
313
+ }
314
+ });
300
315
  };
301
316
 
302
317
  pathRequiresAccessToken = path => {
@@ -306,7 +321,7 @@ class AuthMiddleware {
306
321
  );
307
322
  };
308
323
 
309
- proxyToAuthServer = async (req, path, body) => {
324
+ proxyToAuthServer = async (req, path) => {
310
325
  let accessToken = null;
311
326
  if (this.pathRequiresAccessToken(path)) {
312
327
  console.log('path requires access token');
@@ -316,8 +331,16 @@ class AuthMiddleware {
316
331
  }
317
332
  }
318
333
 
334
+ const formData = new FormData();
335
+ if (req.file) {
336
+ formData.append('file', req.file.buffer, {
337
+ filename: req.file.originalname,
338
+ contentType: req.file.mimetype,
339
+ });
340
+ }
341
+
319
342
  let headers = {
320
- 'Content-Type': 'application/json',
343
+ ...formData.getHeaders(),
321
344
  };
322
345
 
323
346
  if (accessToken) {
@@ -330,28 +353,21 @@ class AuthMiddleware {
330
353
  formattedAuthUrl = `${formattedAuthUrl}&clientId=${this.options.clientId}&redirectUri=${this.options.redirectUri}`;
331
354
  }
332
355
 
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
-
341
356
  console.log('formattedAuthUrl:', formattedAuthUrl);
342
357
  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));
358
+ console.log('file:', req.file);
359
+ console.log('formData:', formData);
360
+ console.log('headers:', headers);
361
+
362
+ const data = req.file ? formData : req.body;
363
+
347
364
  return axios({
348
365
  method: req.method,
349
366
  url: `${formattedAuthUrl}${path}`,
350
- data: req.body,
351
- headers: {
352
- ...headers,
353
- ...formData.getHeaders(),
354
- },
367
+ data: data,
368
+ headers: headers,
369
+ maxContentLength: Infinity,
370
+ maxBodyLength: Infinity,
355
371
  });
356
372
  };
357
373