@quickbiteapp/shared 1.0.20 → 1.0.21
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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const JWT_SECRET: string | undefined;
|
package/build/config.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.JWT_SECRET = void 0;
|
|
7
|
+
const dotenv_1 = __importDefault(require("dotenv"));
|
|
8
|
+
const NotAuthenticatedError_1 = require("./errors/NotAuthenticatedError");
|
|
9
|
+
dotenv_1.default.config();
|
|
10
|
+
exports.JWT_SECRET = process.env.JWT_SECRET;
|
|
11
|
+
if (!exports.JWT_SECRET) {
|
|
12
|
+
throw new NotAuthenticatedError_1.NotAuthenticatedError("JWT_SECRET is not defined in environment");
|
|
13
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
|
2
|
-
export declare const requireAuth: (
|
|
2
|
+
export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
|
|
@@ -6,22 +6,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.requireAuth = void 0;
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
8
|
const NotAuthenticatedError_1 = require("../errors/NotAuthenticatedError");
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
9
|
+
const SecretNotDefinedError_1 = require("../errors/SecretNotDefinedError");
|
|
10
|
+
const config_1 = require("../config");
|
|
11
|
+
const requireAuth = (req, res, next) => {
|
|
12
|
+
const jwtKey = config_1.JWT_SECRET;
|
|
13
|
+
if (!jwtKey) {
|
|
14
|
+
throw new SecretNotDefinedError_1.SecretNotDefinedError();
|
|
15
|
+
}
|
|
16
|
+
const authHeader = req.headers.authorization;
|
|
17
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
18
|
+
throw new NotAuthenticatedError_1.NotAuthenticatedError();
|
|
19
|
+
}
|
|
20
|
+
const token = authHeader.split(" ")[1];
|
|
21
|
+
try {
|
|
22
|
+
const payload = jsonwebtoken_1.default.verify(token, jwtKey);
|
|
23
|
+
req.user = payload;
|
|
24
|
+
next();
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
console.log(err);
|
|
28
|
+
next(new NotAuthenticatedError_1.NotAuthenticatedError());
|
|
29
|
+
}
|
|
26
30
|
};
|
|
27
31
|
exports.requireAuth = requireAuth;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quickbiteapp/shared",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.21",
|
|
4
4
|
"main": "./build/index.js",
|
|
5
5
|
"types": "./build/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"license": "ISC",
|
|
20
20
|
"description": "",
|
|
21
21
|
"dependencies": {
|
|
22
|
+
"dotenv": "^17.2.4",
|
|
22
23
|
"express": "^5.2.1",
|
|
23
24
|
"express-validator": "^7.3.1",
|
|
24
25
|
"jsonwebtoken": "^9.0.3"
|