elseware-nodejs 1.4.0 → 1.5.0

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/dist/index.cjs CHANGED
@@ -5,6 +5,7 @@ var fs = require('fs');
5
5
  var cloudinaryModule = require('cloudinary');
6
6
  var mongoose = require('mongoose');
7
7
  var dotenv = require('dotenv');
8
+ var jwt = require('jsonwebtoken');
8
9
 
9
10
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
11
 
@@ -12,6 +13,7 @@ var fs__default = /*#__PURE__*/_interopDefault(fs);
12
13
  var cloudinaryModule__default = /*#__PURE__*/_interopDefault(cloudinaryModule);
13
14
  var mongoose__default = /*#__PURE__*/_interopDefault(mongoose);
14
15
  var dotenv__default = /*#__PURE__*/_interopDefault(dotenv);
16
+ var jwt__default = /*#__PURE__*/_interopDefault(jwt);
15
17
 
16
18
  // src/errors/appError.ts
17
19
  var AppError = class extends Error {
@@ -5295,51 +5297,96 @@ var validate = (schema) => (req, _res, next) => {
5295
5297
  }
5296
5298
  next();
5297
5299
  };
5298
-
5299
- // src/services/base.service.ts
5300
- var BaseService = class {
5301
- model;
5302
- resourceName;
5303
- constructor(model, resourceName) {
5304
- this.model = model;
5305
- this.resourceName = resourceName;
5306
- }
5307
- async create(data) {
5308
- const doc = await this.model.create(data);
5309
- return doc;
5310
- }
5311
- async findAll() {
5312
- return this.model.find();
5313
- }
5314
- async findById(id) {
5315
- const doc = await this.model.findById(id);
5316
- if (!doc) {
5317
- throw new AppError(`${this.resourceName} not found`, 404, {
5318
- code: `${this.resourceName.toUpperCase()}_NOT_FOUND`
5319
- });
5320
- }
5321
- return doc;
5300
+ var JWTService = class {
5301
+ accessSecret;
5302
+ refreshSecret;
5303
+ accessExpires;
5304
+ refreshExpires;
5305
+ nodeEnv;
5306
+ refreshCookieName;
5307
+ constructor(options) {
5308
+ this.accessSecret = options.accessSecret;
5309
+ this.refreshSecret = options.refreshSecret;
5310
+ this.accessExpires = options.accessExpires || "15m";
5311
+ this.refreshExpires = options.refreshExpires || "30d";
5312
+ this.nodeEnv = options.nodeEnv || "development";
5313
+ this.refreshCookieName = options.refreshCookieName || "refreshJwt";
5314
+ }
5315
+ // ---------------------------------
5316
+ // Access and Refresh Token Creation
5317
+ // ---------------------------------
5318
+ createAccessToken(payload) {
5319
+ return jwt__default.default.sign(payload, this.accessSecret, {
5320
+ expiresIn: this.accessExpires
5321
+ });
5322
5322
  }
5323
- async updateById(id, data) {
5324
- const doc = await this.model.findByIdAndUpdate(id, data, {
5325
- new: true,
5326
- runValidators: true
5323
+ createRefreshToken(payload) {
5324
+ return jwt__default.default.sign(payload, this.refreshSecret, {
5325
+ expiresIn: this.refreshExpires
5327
5326
  });
5328
- if (!doc) {
5329
- throw new AppError(`${this.resourceName} not found`, 404, {
5330
- code: `${this.resourceName.toUpperCase()}_NOT_FOUND`
5331
- });
5327
+ }
5328
+ // ---------------------------------
5329
+ // Token Verification
5330
+ // ---------------------------------
5331
+ verifyAccessToken(token) {
5332
+ try {
5333
+ return jwt__default.default.verify(token, this.accessSecret);
5334
+ } catch (err) {
5335
+ console.error("Access token verification failed:", err.message);
5336
+ return null;
5332
5337
  }
5333
- return doc;
5334
5338
  }
5335
- async deleteById(id) {
5336
- const doc = await this.model.findByIdAndDelete(id);
5337
- if (!doc) {
5338
- throw new AppError(`${this.resourceName} not found`, 404, {
5339
- code: `${this.resourceName.toUpperCase()}_NOT_FOUND`
5340
- });
5339
+ verifyRefreshToken(token) {
5340
+ try {
5341
+ return jwt__default.default.verify(token, this.refreshSecret);
5342
+ } catch (err) {
5343
+ console.error("Refresh token verification failed:", err.message);
5344
+ return null;
5341
5345
  }
5342
- return doc;
5346
+ }
5347
+ // ---------------------------------
5348
+ // Token Extraction
5349
+ // ---------------------------------
5350
+ extractToken(req) {
5351
+ if (req.headers.authorization?.startsWith("Bearer ")) {
5352
+ return req.headers.authorization.split(" ")[1];
5353
+ }
5354
+ if (req.cookies?.[this.refreshCookieName]) {
5355
+ return req.cookies[this.refreshCookieName];
5356
+ }
5357
+ if (req.cookies?.jwt) {
5358
+ return req.cookies.jwt;
5359
+ }
5360
+ return null;
5361
+ }
5362
+ // ---------------------------------
5363
+ // Cookie Helpers
5364
+ // ---------------------------------
5365
+ setRefreshTokenCookie(res, refreshToken) {
5366
+ res.cookie(this.refreshCookieName, refreshToken, {
5367
+ httpOnly: true,
5368
+ secure: this.nodeEnv === "production",
5369
+ sameSite: this.nodeEnv === "production" ? "none" : "lax",
5370
+ maxAge: 30 * 24 * 60 * 60 * 1e3,
5371
+ path: "/"
5372
+ });
5373
+ }
5374
+ clearRefreshTokenCookie(res) {
5375
+ res.clearCookie(this.refreshCookieName, {
5376
+ httpOnly: true,
5377
+ secure: this.nodeEnv === "production",
5378
+ sameSite: this.nodeEnv === "production" ? "none" : "lax",
5379
+ path: "/"
5380
+ });
5381
+ }
5382
+ // ---------------------------------
5383
+ // Login Helpers
5384
+ // ---------------------------------
5385
+ sendAuthTokens(res, payload) {
5386
+ const accessToken = this.createAccessToken(payload);
5387
+ const refreshToken = this.createRefreshToken(payload);
5388
+ this.setRefreshTokenCookie(res, refreshToken);
5389
+ return accessToken;
5343
5390
  }
5344
5391
  };
5345
5392
 
@@ -5382,7 +5429,6 @@ exports.AppError = AppError;
5382
5429
  exports.AzureBlobService = AzureBlobService;
5383
5430
  exports.BPlusTree = BPlusTree;
5384
5431
  exports.BTree = BTree;
5385
- exports.BaseService = BaseService;
5386
5432
  exports.BinaryHeap = BinaryHeap;
5387
5433
  exports.BinarySearchTree = BinarySearchTree;
5388
5434
  exports.BinaryTree = BinaryTree;
@@ -5407,6 +5453,7 @@ exports.HashMap = HashMap;
5407
5453
  exports.HashSet = HashSet;
5408
5454
  exports.HyperLogLog = HyperLogLog;
5409
5455
  exports.IntervalTree = IntervalTree;
5456
+ exports.JWTService = JWTService;
5410
5457
  exports.KDTree = KDTree;
5411
5458
  exports.LFUCache = LFUCache;
5412
5459
  exports.LRUCache = LRUCache;