@quickbiteapp/shared 1.0.13 → 1.0.15

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,4 @@
1
+ import { AppError } from "./AppError";
2
+ export declare class NotAuthenticatedError extends AppError {
3
+ constructor(message?: string);
4
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotAuthenticatedError = void 0;
4
+ const AppError_1 = require("./AppError");
5
+ class NotAuthenticatedError extends AppError_1.AppError {
6
+ constructor(message = "Not authenticated") {
7
+ super(message, 401);
8
+ }
9
+ }
10
+ exports.NotAuthenticatedError = NotAuthenticatedError;
@@ -0,0 +1,4 @@
1
+ import { AppError } from "./AppError";
2
+ export declare class NotAuthorizedError extends AppError {
3
+ constructor(message?: string);
4
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotAuthorizedError = void 0;
4
+ const AppError_1 = require("./AppError");
5
+ class NotAuthorizedError extends AppError_1.AppError {
6
+ constructor(message = "Not authorized") {
7
+ super(message, 401);
8
+ }
9
+ }
10
+ exports.NotAuthorizedError = NotAuthorizedError;
@@ -0,0 +1,4 @@
1
+ import { AppError } from "./AppError";
2
+ export declare class SecretNotDefinedError extends AppError {
3
+ constructor(message?: string);
4
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SecretNotDefinedError = void 0;
4
+ const AppError_1 = require("./AppError");
5
+ class SecretNotDefinedError extends AppError_1.AppError {
6
+ constructor(message = "Secret is not defined") {
7
+ super(message, 500);
8
+ }
9
+ }
10
+ exports.SecretNotDefinedError = SecretNotDefinedError;
package/build/index.d.ts CHANGED
@@ -7,5 +7,8 @@ export * from './errors/RequestValidationError';
7
7
  export * from './errors/RequiredFieldError';
8
8
  export * from './errors/InvalidCredentialsError';
9
9
  export * from './errors/UserNotActiveError';
10
+ export * from './errors/SecretNotDefinedError';
10
11
  export * from './middlewares/ErrorHandler';
11
12
  export * from './middlewares/ValidateRequest';
13
+ export * from './middlewares/RequireAuth';
14
+ export * from './middlewares/RequireSuperAdmin';
package/build/index.js CHANGED
@@ -23,5 +23,8 @@ __exportStar(require("./errors/RequestValidationError"), exports);
23
23
  __exportStar(require("./errors/RequiredFieldError"), exports);
24
24
  __exportStar(require("./errors/InvalidCredentialsError"), exports);
25
25
  __exportStar(require("./errors/UserNotActiveError"), exports);
26
+ __exportStar(require("./errors/SecretNotDefinedError"), exports);
26
27
  __exportStar(require("./middlewares/ErrorHandler"), exports);
27
28
  __exportStar(require("./middlewares/ValidateRequest"), exports);
29
+ __exportStar(require("./middlewares/RequireAuth"), exports);
30
+ __exportStar(require("./middlewares/RequireSuperAdmin"), exports);
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,29 @@
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.requireAuth = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const NotAuthenticatedError_1 = require("../errors/NotAuthenticatedError");
9
+ const SecretNotDefinedError_1 = require("../errors/SecretNotDefinedError");
10
+ const requireAuth = (req, res, next) => {
11
+ const jwtKey = process.env.JWT_SECRET;
12
+ if (!jwtKey) {
13
+ throw new SecretNotDefinedError_1.SecretNotDefinedError();
14
+ }
15
+ const authHeader = req.headers.authorization;
16
+ if (!authHeader || !authHeader.startsWith("Bearer ")) {
17
+ throw new NotAuthenticatedError_1.NotAuthenticatedError();
18
+ }
19
+ const token = authHeader.split(" ")[1];
20
+ try {
21
+ const payload = jsonwebtoken_1.default.verify(token, jwtKey);
22
+ req.user = payload;
23
+ next();
24
+ }
25
+ catch (_a) {
26
+ next(new NotAuthenticatedError_1.NotAuthenticatedError("Not authorized"));
27
+ }
28
+ };
29
+ exports.requireAuth = requireAuth;
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const requireSuperAdmin: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireSuperAdmin = void 0;
4
+ const NotAuthorizedError_1 = require("../errors/NotAuthorizedError");
5
+ const requireSuperAdmin = (req, res, next) => {
6
+ var _a;
7
+ if (((_a = req.user) === null || _a === void 0 ? void 0 : _a.role) !== "SuperAdmin") {
8
+ throw new NotAuthorizedError_1.NotAuthorizedError();
9
+ }
10
+ next();
11
+ };
12
+ exports.requireSuperAdmin = requireSuperAdmin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quickbiteapp/shared",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -20,10 +20,12 @@
20
20
  "description": "",
21
21
  "dependencies": {
22
22
  "express": "^5.2.1",
23
- "express-validator": "^7.3.1"
23
+ "express-validator": "^7.3.1",
24
+ "jsonwebtoken": "^9.0.3"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@types/express": "^5.0.6",
28
+ "@types/jsonwebtoken": "^9.0.10",
27
29
  "del-cli": "^7.0.0",
28
30
  "rimraf": "^6.1.2",
29
31
  "typescript": "^5.9.3"