@sendhome/common 1.0.155 → 1.0.158

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.
package/build/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export * from "./errors/database-connection-error";
4
4
  export * from "./errors/not-authorized-error";
5
5
  export * from "./errors/not-found-error";
6
6
  export * from "./errors/request-validation-error";
7
+ export * from "./middlewares/check-user-access";
7
8
  export * from "./middlewares/current-user";
8
9
  export * from "./middlewares/error-handler";
9
10
  export * from "./middlewares/require-auth";
package/build/index.js CHANGED
@@ -17,6 +17,7 @@ __exportStar(require("./errors/database-connection-error"), exports);
17
17
  __exportStar(require("./errors/not-authorized-error"), exports);
18
18
  __exportStar(require("./errors/not-found-error"), exports);
19
19
  __exportStar(require("./errors/request-validation-error"), exports);
20
+ __exportStar(require("./middlewares/check-user-access"), exports);
20
21
  __exportStar(require("./middlewares/current-user"), exports);
21
22
  __exportStar(require("./middlewares/error-handler"), exports);
22
23
  __exportStar(require("./middlewares/require-auth"), exports);
@@ -0,0 +1,20 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ interface UserPayload {
3
+ id: string;
4
+ sessionId: string;
5
+ account_type: string;
6
+ provider_level?: string;
7
+ admin_level?: string;
8
+ }
9
+ declare global {
10
+ namespace Express {
11
+ interface Request {
12
+ currentUser?: UserPayload;
13
+ }
14
+ }
15
+ }
16
+ export declare const checkUserAccess: (requiredAccountType: string, requiredLevels?: {
17
+ provider?: string[] | undefined;
18
+ admin?: string[] | undefined;
19
+ } | undefined) => (req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
20
+ export {};
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkUserAccess = void 0;
4
+ const checkUserAccess = (requiredAccountType, requiredLevels) => {
5
+ return (req, res, next) => {
6
+ const { account_type, provider_level = '', admin_level = '' } = req.currentUser; // Default to empty string if undefined
7
+ // Check if the user's account_type matches the required account_type
8
+ if (account_type !== requiredAccountType) {
9
+ return res.status(403).send({ error: 'You do not have the necessary permissions to access this resource' });
10
+ }
11
+ // Check provider levels for 'provider' users
12
+ if (account_type === 'provider' && (requiredLevels === null || requiredLevels === void 0 ? void 0 : requiredLevels.provider)) {
13
+ if (!requiredLevels.provider.includes(provider_level)) {
14
+ return res.status(403).send({ error: 'Provider level not authorized for this route' });
15
+ }
16
+ }
17
+ // Check admin levels for 'admin' users
18
+ if (account_type === 'admin' && (requiredLevels === null || requiredLevels === void 0 ? void 0 : requiredLevels.admin)) {
19
+ if (!requiredLevels.admin.includes(admin_level)) {
20
+ return res.status(403).send({ error: 'Admin level not authorized for this route' });
21
+ }
22
+ }
23
+ next();
24
+ };
25
+ };
26
+ exports.checkUserAccess = checkUserAccess;
@@ -1,8 +1,10 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
2
  interface UserPayload {
3
3
  id: string;
4
- email: string;
5
4
  sessionId: string;
5
+ account_type: string;
6
+ provider_level?: string;
7
+ admin_level?: string;
6
8
  }
7
9
  declare global {
8
10
  namespace Express {
@@ -1,8 +1,10 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
2
  interface UserPayload {
3
3
  id: string;
4
- email: string;
5
4
  sessionId: string;
5
+ account_type: string;
6
+ provider_level?: string;
7
+ admin_level?: string;
6
8
  }
7
9
  declare global {
8
10
  namespace Express {
@@ -1,8 +1,10 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
2
  interface UserPayload {
3
3
  id: string;
4
- email: string;
5
4
  sessionId: string;
5
+ account_type: string;
6
+ provider_level?: string;
7
+ admin_level?: string;
6
8
  }
7
9
  declare global {
8
10
  namespace Express {
@@ -11,5 +13,5 @@ declare global {
11
13
  }
12
14
  }
13
15
  }
14
- export declare const requireAuth: (sessionServiceUrl: string, deviceInfo: string) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
16
+ export declare const requireAuth: (sessionServiceUrl: string) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
15
17
  export {};
@@ -16,7 +16,7 @@ exports.requireAuth = void 0;
16
16
  const axios_1 = __importDefault(require("axios"));
17
17
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
18
18
  // Middleware to validate user session per device
19
- const requireAuth = (sessionServiceUrl, deviceInfo) => {
19
+ const requireAuth = (sessionServiceUrl) => {
20
20
  return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
21
21
  var _a;
22
22
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sendhome/common",
3
- "version": "1.0.155",
3
+ "version": "1.0.158",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",