@medi-car-bd/mcb-shared-auth 0.0.4
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/LICENSE +21 -0
- package/README.md +50 -0
- package/dist/config/env.d.ts +5 -0
- package/dist/config/env.js +10 -0
- package/dist/config/index.d.ts +1 -0
- package/dist/config/index.js +17 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/middlewares/authenticate.d.ts +6 -0
- package/dist/middlewares/authenticate.js +26 -0
- package/dist/middlewares/authorize.d.ts +3 -0
- package/dist/middlewares/authorize.js +15 -0
- package/dist/middlewares/index.d.ts +3 -0
- package/dist/middlewares/index.js +19 -0
- package/dist/types/commonType.d.ts +3 -0
- package/dist/types/commonType.js +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +18 -0
- package/dist/types/userType.d.ts +9 -0
- package/dist/types/userType.js +2 -0
- package/dist/utils/appError.d.ts +11 -0
- package/dist/utils/appError.js +14 -0
- package/dist/utils/catchAsync.d.ts +3 -0
- package/dist/utils/catchAsync.js +9 -0
- package/dist/utils/comparePassword.d.ts +1 -0
- package/dist/utils/comparePassword.js +12 -0
- package/dist/utils/globalError.d.ts +6 -0
- package/dist/utils/globalError.js +79 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.js +23 -0
- package/dist/utils/limiter.d.ts +1 -0
- package/dist/utils/limiter.js +12 -0
- package/dist/utils/logger.d.ts +2 -0
- package/dist/utils/logger.js +38 -0
- package/dist/utils/signToken.d.ts +2 -0
- package/dist/utils/signToken.js +19 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 FatihOnlinEShopBD
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# mcb-backend-boilerplate
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
rm -r node_modules package-lock.json
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
- Prisma Generate
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm prisma generate
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
- Run Prisma To Initialize Database
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm prisma init
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- Generate Prisma Client
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm prisma generate
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- Run Prisma Migrations
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm prisma:migrate
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- Shared Auth package installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
set GITHUB_TOKEN="your_github_token"
|
|
36
|
+
|
|
37
|
+
pnpm install @mcb-shared-auth/auth
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Or Use
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pnpm install github:Medi-Car-BD/mcb-shared-auth
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Patched version of the shared auth package
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm version patch
|
|
50
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JWT_EXPIRES_IN = exports.JWT_SECRET = exports.NODE_ENV = void 0;
|
|
4
|
+
require("dotenv/config");
|
|
5
|
+
const NODE_ENV = process.env.NODE_ENV;
|
|
6
|
+
exports.NODE_ENV = NODE_ENV;
|
|
7
|
+
const JWT_SECRET = process.env.JWT_SECRET;
|
|
8
|
+
exports.JWT_SECRET = JWT_SECRET;
|
|
9
|
+
const JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN;
|
|
10
|
+
exports.JWT_EXPIRES_IN = JWT_EXPIRES_IN;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./env";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./env"), exports);
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./utils"), exports);
|
|
18
|
+
__exportStar(require("./middlewares"), exports);
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from "express";
|
|
2
|
+
import jwt from "jsonwebtoken";
|
|
3
|
+
export type AuthenticatedRequest = Request & {
|
|
4
|
+
user?: jwt.JwtPayload & Record<string, unknown>;
|
|
5
|
+
};
|
|
6
|
+
export declare const authenticate: (req: AuthenticatedRequest, _res: Response, next: NextFunction) => void;
|
|
@@ -0,0 +1,26 @@
|
|
|
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.authenticate = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const config_1 = require("../config");
|
|
9
|
+
const utils_1 = require("../utils");
|
|
10
|
+
const authenticate = (req, _res, next) => {
|
|
11
|
+
const header = req.headers.authorization;
|
|
12
|
+
const token = (header && header.startsWith("Bearer ") && header.split(" ")[1]) ||
|
|
13
|
+
req.headers["x-access-token"];
|
|
14
|
+
if (!token) {
|
|
15
|
+
return next(new utils_1.AppError("Authentication required", 401));
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
const decoded = jsonwebtoken_1.default.verify(token, String(config_1.JWT_SECRET));
|
|
19
|
+
req.user = decoded;
|
|
20
|
+
return next();
|
|
21
|
+
}
|
|
22
|
+
catch (_a) {
|
|
23
|
+
return next(new utils_1.AppError("Invalid or expired token", 401));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
exports.authenticate = authenticate;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.authorize = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const authorize = (...roles) => (req, _res, next) => {
|
|
6
|
+
var _a;
|
|
7
|
+
const role = (_a = req.user) === null || _a === void 0 ? void 0 : _a.role;
|
|
8
|
+
if (roles.length === 0)
|
|
9
|
+
return next();
|
|
10
|
+
if (!role || !roles.includes(role)) {
|
|
11
|
+
return next(new utils_1.AppError("Forbidden", 403));
|
|
12
|
+
}
|
|
13
|
+
return next();
|
|
14
|
+
};
|
|
15
|
+
exports.authorize = authorize;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("../utils/globalError"), exports);
|
|
18
|
+
__exportStar(require("./authenticate"), exports);
|
|
19
|
+
__exportStar(require("./authorize"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./userType"), exports);
|
|
18
|
+
__exportStar(require("./commonType"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class AppError extends Error {
|
|
2
|
+
statusCode: number;
|
|
3
|
+
status: "fail" | "error";
|
|
4
|
+
isOperational: boolean;
|
|
5
|
+
errmsg?: string;
|
|
6
|
+
code?: number;
|
|
7
|
+
errors?: Record<string, unknown>;
|
|
8
|
+
path?: string;
|
|
9
|
+
value?: string | number;
|
|
10
|
+
constructor(message: string, statusCode: number);
|
|
11
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppError = void 0;
|
|
4
|
+
class AppError extends Error {
|
|
5
|
+
constructor(message, statusCode) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.statusCode = statusCode;
|
|
8
|
+
this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
|
|
9
|
+
this.isOperational = true;
|
|
10
|
+
this.errmsg = message;
|
|
11
|
+
this.code = statusCode;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.AppError = AppError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const comparePassword: (password: string, userPassword: string) => boolean;
|
|
@@ -0,0 +1,12 @@
|
|
|
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.comparePassword = void 0;
|
|
7
|
+
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
|
8
|
+
// compare password from request with the one in the database
|
|
9
|
+
const comparePassword = (password, userPassword) => {
|
|
10
|
+
return bcryptjs_1.default.compareSync(password, userPassword);
|
|
11
|
+
};
|
|
12
|
+
exports.comparePassword = comparePassword;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AppError } from ".";
|
|
2
|
+
import type { Request, Response, NextFunction } from "express";
|
|
3
|
+
declare const processRequest: (req: Request, res: Response, next: NextFunction) => void;
|
|
4
|
+
declare const notFound: (req: Request, res: Response, next: NextFunction) => void;
|
|
5
|
+
declare const errorHandler: (err: AppError, req: Request, res: Response, _next: NextFunction) => void;
|
|
6
|
+
export { notFound, errorHandler, processRequest };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processRequest = exports.errorHandler = exports.notFound = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
5
|
+
const processRequest = (req, res, next) => {
|
|
6
|
+
const corrHeader = req.headers["x-correlation-id"];
|
|
7
|
+
const correlationId = Array.isArray(corrHeader)
|
|
8
|
+
? corrHeader.join(",")
|
|
9
|
+
: (corrHeader !== null && corrHeader !== void 0 ? corrHeader : "");
|
|
10
|
+
const requestBody = req.body;
|
|
11
|
+
_1.logger.info(`${req.originalUrl} - ${req.method} - ${req.ip} - ${correlationId} - ${JSON.stringify(requestBody)}`);
|
|
12
|
+
next();
|
|
13
|
+
};
|
|
14
|
+
exports.processRequest = processRequest;
|
|
15
|
+
// create a global error handler middleware
|
|
16
|
+
const notFound = (req, res, next) => {
|
|
17
|
+
// handle error for /favicon.ico request from browser to server, we have ico on public folder
|
|
18
|
+
const error = new _1.AppError(`Not found - ${req.originalUrl}`, 404);
|
|
19
|
+
next(error);
|
|
20
|
+
};
|
|
21
|
+
exports.notFound = notFound;
|
|
22
|
+
// create a global error handler middleware
|
|
23
|
+
const errorHandler = (err, req, res, _next) => {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
const corrHeader = req.headers["x-correlation-id"];
|
|
26
|
+
const correlationId = Array.isArray(corrHeader)
|
|
27
|
+
? corrHeader.join(",")
|
|
28
|
+
: (corrHeader !== null && corrHeader !== void 0 ? corrHeader : "");
|
|
29
|
+
let statusCode = err.statusCode || 500;
|
|
30
|
+
const status = err.status || "error";
|
|
31
|
+
let { message } = err;
|
|
32
|
+
if (err.code === 11000) {
|
|
33
|
+
statusCode = 400;
|
|
34
|
+
const value = ((_b = (_a = err.errmsg) === null || _a === void 0 ? void 0 : _a.match(/(["'])(?:\\.|[^\\])*?\1/)) === null || _b === void 0 ? void 0 : _b[0]) || "value";
|
|
35
|
+
message = `Duplicate field value: ${value}. Please use another value!`;
|
|
36
|
+
}
|
|
37
|
+
// use switch case instead of if else for err.name
|
|
38
|
+
switch (err.name) {
|
|
39
|
+
case "CastError":
|
|
40
|
+
statusCode = 400;
|
|
41
|
+
message = `Invalid ${err.path}: ${err.value}`;
|
|
42
|
+
break;
|
|
43
|
+
case "PostgresServerError": {
|
|
44
|
+
const e = (err.stack || "").split(":");
|
|
45
|
+
message = `${e[1] || "Postgres server error"}`;
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case "ValidationError": {
|
|
49
|
+
const errors = Object.values(err.errors || {}).map((el) => { var _a; return String((_a = el === null || el === void 0 ? void 0 : el.message) !== null && _a !== void 0 ? _a : el); });
|
|
50
|
+
message = `Invalid input data. ${errors.join(". ")}`;
|
|
51
|
+
statusCode = 400;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case "SyntaxError":
|
|
55
|
+
message = `Invalid api request. ${err.message}`;
|
|
56
|
+
statusCode = 400;
|
|
57
|
+
break;
|
|
58
|
+
case "JsonWebTokenError":
|
|
59
|
+
message = `Invalid token. Please log in again!`;
|
|
60
|
+
statusCode = 401;
|
|
61
|
+
break;
|
|
62
|
+
case "TokenExpiredError":
|
|
63
|
+
message = `Your token has expired!. Please log in again!`;
|
|
64
|
+
statusCode = 401;
|
|
65
|
+
break;
|
|
66
|
+
case "ReferenceError":
|
|
67
|
+
statusCode = 500;
|
|
68
|
+
break;
|
|
69
|
+
default:
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
res.status(statusCode).json({
|
|
73
|
+
state: status,
|
|
74
|
+
message,
|
|
75
|
+
stack: err.stack,
|
|
76
|
+
});
|
|
77
|
+
_1.logger.error(`${statusCode} - ${status} - ${req.originalUrl} - ${req.method} - ${req.ip} - ${correlationId} - ${err.message} - ${err.stack} `);
|
|
78
|
+
};
|
|
79
|
+
exports.errorHandler = errorHandler;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./catchAsync"), exports);
|
|
18
|
+
__exportStar(require("./appError"), exports);
|
|
19
|
+
__exportStar(require("./logger"), exports);
|
|
20
|
+
__exportStar(require("./comparePassword"), exports);
|
|
21
|
+
__exportStar(require("./signToken"), exports);
|
|
22
|
+
__exportStar(require("./globalError"), exports);
|
|
23
|
+
__exportStar(require("./limiter"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const limiter: import("express-rate-limit").RateLimitRequestHandler;
|
|
@@ -0,0 +1,12 @@
|
|
|
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.limiter = void 0;
|
|
7
|
+
const express_rate_limit_1 = __importDefault(require("express-rate-limit"));
|
|
8
|
+
exports.limiter = (0, express_rate_limit_1.default)({
|
|
9
|
+
max: 100, // 100 requests
|
|
10
|
+
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
11
|
+
message: "Too many requests from this IP, please try again in an hour!", // message to send
|
|
12
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = void 0;
|
|
4
|
+
const winston_1 = require("winston");
|
|
5
|
+
require("winston-daily-rotate-file");
|
|
6
|
+
const config_1 = require("../config");
|
|
7
|
+
// create logger with a daily rotate file transport with proper type and generics if needed
|
|
8
|
+
exports.logger = (0, winston_1.createLogger)({
|
|
9
|
+
level: "info",
|
|
10
|
+
format: winston_1.format.combine(winston_1.format.timestamp({
|
|
11
|
+
format: "YYYY-MM-DD HH:mm:ss",
|
|
12
|
+
}), winston_1.format.json()),
|
|
13
|
+
transports: [
|
|
14
|
+
new winston_1.transports.DailyRotateFile({
|
|
15
|
+
filename: "logs/error/%DATE%.log",
|
|
16
|
+
datePattern: "YYYY-MM-DD",
|
|
17
|
+
zippedArchive: true,
|
|
18
|
+
maxSize: "20m",
|
|
19
|
+
maxFiles: "14d",
|
|
20
|
+
level: "error",
|
|
21
|
+
}),
|
|
22
|
+
new winston_1.transports.DailyRotateFile({
|
|
23
|
+
filename: "logs/%DATE%.log",
|
|
24
|
+
datePattern: "YYYY-MM-DD",
|
|
25
|
+
zippedArchive: true,
|
|
26
|
+
maxSize: "20m",
|
|
27
|
+
maxFiles: "14d",
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
if (config_1.NODE_ENV !== "production") {
|
|
32
|
+
exports.logger.add(new winston_1.transports.Console({
|
|
33
|
+
format: winston_1.format.combine(winston_1.format.colorize(), winston_1.format.timestamp({
|
|
34
|
+
format: "YYYY-MM-DD HH:mm:ss",
|
|
35
|
+
}), winston_1.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)),
|
|
36
|
+
level: "debug",
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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.signToken = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const config_1 = require("../config");
|
|
9
|
+
const signToken = (user) => {
|
|
10
|
+
return jsonwebtoken_1.default.sign({
|
|
11
|
+
id: user.id,
|
|
12
|
+
name: user.name,
|
|
13
|
+
email: user.email,
|
|
14
|
+
password: user.password,
|
|
15
|
+
}, config_1.JWT_SECRET, {
|
|
16
|
+
expiresIn: config_1.JWT_EXPIRES_IN,
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
exports.signToken = signToken;
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@medi-car-bd/mcb-shared-auth",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Shared authentication module for MCB applications",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"preinstall": "node -e \"\"",
|
|
12
|
+
"postinstall": "",
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"start": "node dist/index.js",
|
|
15
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
16
|
+
"lint": "eslint . --ext .ts,.tsx,.js,.jsx",
|
|
17
|
+
"lint:fix": "eslint . --ext .ts,.tsx,.js,.jsx --fix",
|
|
18
|
+
"format": "prettier --write .",
|
|
19
|
+
"format:check": "prettier --check ."
|
|
20
|
+
},
|
|
21
|
+
"keywords": [],
|
|
22
|
+
"author": "MediCarBD",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/express": "^4.17.21",
|
|
26
|
+
"@types/node": "^20.11.0",
|
|
27
|
+
"@typescript-eslint/eslint-plugin": "^8.54.0",
|
|
28
|
+
"@typescript-eslint/parser": "^8.54.0",
|
|
29
|
+
"eslint": "^9.39.2",
|
|
30
|
+
"eslint-config-prettier": "^9.1.0",
|
|
31
|
+
"prettier": "^3.2.5",
|
|
32
|
+
"typescript": "^5.3.3"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"bcryptjs": "^2.4.3",
|
|
36
|
+
"dotenv": "^16.3.1",
|
|
37
|
+
"express-rate-limit": "^7.1.5",
|
|
38
|
+
"jsonwebtoken": "^9.0.2",
|
|
39
|
+
"winston": "^3.11.0",
|
|
40
|
+
"winston-daily-rotate-file": "^4.7.1"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"express": "^4.18.2"
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"dist"
|
|
47
|
+
],
|
|
48
|
+
"lint-staged": {
|
|
49
|
+
"*.{ts,tsx,js,jsx}": [
|
|
50
|
+
"eslint --max-warnings=0 --fix",
|
|
51
|
+
"prettier --write"
|
|
52
|
+
],
|
|
53
|
+
"*.{json,md,yml,yaml}": [
|
|
54
|
+
"prettier --write"
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "git+https://github.com/Medi-Car-BD/mcb-shared-auth"
|
|
60
|
+
}
|
|
61
|
+
}
|