@uber-clone/common 1.0.12 → 1.0.13

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.
@@ -6,12 +6,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.currentUser = void 0;
7
7
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
8
  const currentUser = (req, res, next) => {
9
- var _a;
10
- if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
9
+ var _a, _b;
10
+ let token;
11
+ // 1. Try cookie-session first
12
+ if ((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt) {
13
+ token = req.session.jwt;
14
+ }
15
+ // 2. Fallback to Authorization: Bearer <token>
16
+ else if ((_b = req.headers.authorization) === null || _b === void 0 ? void 0 : _b.startsWith("Bearer ")) {
17
+ token = req.headers.authorization.split(" ")[1];
18
+ }
19
+ if (!token) {
11
20
  return next();
12
21
  }
13
22
  try {
14
- const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
23
+ const payload = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY);
15
24
  req.currentUser = payload;
16
25
  }
17
26
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uber-clone/common",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [