@hastehaul/common 2.0.32 → 2.0.33

Sign up to get free protection for your applications and to get access to all the features.
@@ -44,33 +44,28 @@ const jsonwebtoken_1 = __importStar(require("jsonwebtoken"));
44
44
  * @returns {Promise<void>} - A Promise that resolves once the user extraction is complete, but it is not used directly.
45
45
  */
46
46
  const currentUser = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
47
- let result = null;
47
+ req.currentUser = null;
48
+ const bearerToken = req.headers.authorization;
49
+ if (!bearerToken) {
50
+ return next();
51
+ }
48
52
  try {
49
- // Extract the Bearer token from the Authorization header.
50
- const bearerToken = req.headers["authorization"];
51
- if (typeof bearerToken !== "undefined" && bearerToken) {
52
- const token = bearerToken.split(" ")[1];
53
- if (token) {
54
- // Verify the JWT token and decode the user payload.
55
- result = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY.split("-").join(""));
56
- }
53
+ const token = bearerToken.split(' ')[1];
54
+ if (token) {
55
+ const result = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY.split('-').join(''));
56
+ req.currentUser = result;
57
57
  }
58
- // Attach the user payload to the request object for later use in the application.
59
- req.currentUser = result;
60
58
  }
61
59
  catch (err) {
62
- // Handle errors related to token verification or expiration.
63
60
  if (err instanceof jsonwebtoken_1.TokenExpiredError) {
64
61
  // If the token is expired, you may consider refreshing the token or taking appropriate actions.
65
62
  // Uncomment the following line if you want to handle token expiration using the AuthController.
66
63
  // await AuthController.refreshToken(req, res);
67
64
  }
68
65
  else {
69
- // If any other error occurs during token verification, set currentUser to null.
70
- req.currentUser = null;
66
+ console.error('Error verifying token:', err);
71
67
  }
72
68
  }
73
- // Call the next middleware in the chain.
74
69
  next();
75
70
  });
76
71
  exports.currentUser = currentUser;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.0.32",
3
+ "version": "2.0.33",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",