@ilman00/authkit 1.0.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.
Files changed (37) hide show
  1. package/dist/config/auth.config.d.ts +4 -0
  2. package/dist/config/auth.config.d.ts.map +1 -0
  3. package/dist/config/auth.config.js +38 -0
  4. package/dist/config/auth.config.js.map +1 -0
  5. package/dist/handlers/auth.handler.d.ts +5 -0
  6. package/dist/handlers/auth.handler.d.ts.map +1 -0
  7. package/dist/handlers/auth.handler.js +36 -0
  8. package/dist/handlers/auth.handler.js.map +1 -0
  9. package/dist/index.d.ts +7 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +14 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/middleware/protect.d.ts +4 -0
  14. package/dist/middleware/protect.d.ts.map +1 -0
  15. package/dist/middleware/protect.js +31 -0
  16. package/dist/middleware/protect.js.map +1 -0
  17. package/dist/services/auth.service.d.ts +10 -0
  18. package/dist/services/auth.service.d.ts.map +1 -0
  19. package/dist/services/auth.service.js +28 -0
  20. package/dist/services/auth.service.js.map +1 -0
  21. package/dist/services/jwt.service.d.ts +6 -0
  22. package/dist/services/jwt.service.d.ts.map +1 -0
  23. package/dist/services/jwt.service.js +25 -0
  24. package/dist/services/jwt.service.js.map +1 -0
  25. package/dist/services/password.service.d.ts +3 -0
  26. package/dist/services/password.service.d.ts.map +1 -0
  27. package/dist/services/password.service.js +16 -0
  28. package/dist/services/password.service.js.map +1 -0
  29. package/dist/types/index.d.ts +22 -0
  30. package/dist/types/index.d.ts.map +1 -0
  31. package/dist/types/index.js +3 -0
  32. package/dist/types/index.js.map +1 -0
  33. package/dist/utils/errors.d.ts +5 -0
  34. package/dist/utils/errors.d.ts.map +1 -0
  35. package/dist/utils/errors.js +12 -0
  36. package/dist/utils/errors.js.map +1 -0
  37. package/package.json +27 -0
@@ -0,0 +1,4 @@
1
+ import { AuthConfig } from "../types";
2
+ export declare function initConfig(userConfig: AuthConfig): void;
3
+ export declare function getConfig(): AuthConfig;
4
+ //# sourceMappingURL=auth.config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.config.d.ts","sourceRoot":"","sources":["../../src/config/auth.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAUtC,wBAAgB,UAAU,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI,CAuBvD;AAED,wBAAgB,SAAS,IAAI,UAAU,CAKtC"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initConfig = initConfig;
4
+ exports.getConfig = getConfig;
5
+ let config = null;
6
+ const REQUIRED_ADAPTER_METHODS = [
7
+ "findUserByEmail",
8
+ "findUserById",
9
+ "createUser",
10
+ ];
11
+ function initConfig(userConfig) {
12
+ // Validate secret
13
+ if (!userConfig.secret) {
14
+ throw new Error("[my-auth-kit] secret is required");
15
+ }
16
+ // Validate adapter
17
+ if (!userConfig.adapter) {
18
+ throw new Error("[my-auth-kit] adapter is required");
19
+ }
20
+ for (const method of REQUIRED_ADAPTER_METHODS) {
21
+ if (typeof userConfig.adapter[method] !== "function") {
22
+ throw new Error(`[my-auth-kit] adapter missing required method: "${method}"`);
23
+ }
24
+ }
25
+ // Store with defaults
26
+ config = {
27
+ accessTokenExpiry: "15m",
28
+ refreshTokenExpiry: "7d",
29
+ ...userConfig,
30
+ };
31
+ }
32
+ function getConfig() {
33
+ if (!config) {
34
+ throw new Error("[my-auth-kit] call init() before using auth");
35
+ }
36
+ return config;
37
+ }
38
+ //# sourceMappingURL=auth.config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.config.js","sourceRoot":"","sources":["../../src/config/auth.config.ts"],"names":[],"mappings":";;AAUA,gCAuBC;AAED,8BAKC;AAtCD,IAAI,MAAM,GAAsB,IAAI,CAAC;AAErC,MAAM,wBAAwB,GAAG;IAC/B,iBAAiB;IACjB,cAAc;IACd,YAAY;CACJ,CAAC;AAEX,SAAgB,UAAU,CAAC,UAAsB;IAC/C,kBAAkB;IAClB,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IACtD,CAAC;IAED,mBAAmB;IACnB,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACvD,CAAC;IAED,KAAK,MAAM,MAAM,IAAI,wBAAwB,EAAE,CAAC;QAC9C,IAAI,OAAO,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,UAAU,EAAE,CAAC;YACrD,MAAM,IAAI,KAAK,CAAC,mDAAmD,MAAM,GAAG,CAAC,CAAC;QAChF,CAAC;IACH,CAAC;IAED,sBAAsB;IACtB,MAAM,GAAG;QACP,iBAAiB,EAAE,KAAK;QACxB,kBAAkB,EAAE,IAAI;QACxB,GAAG,UAAU;KACd,CAAC;AACJ,CAAC;AAED,SAAgB,SAAS;IACvB,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IACjE,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { Response } from "express";
2
+ import { AuthRequest } from "../types";
3
+ export declare function registerHandler(req: AuthRequest, res: Response): Promise<void>;
4
+ export declare function loginHandler(req: AuthRequest, res: Response): Promise<void>;
5
+ //# sourceMappingURL=auth.handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.handler.d.ts","sourceRoot":"","sources":["../../src/handlers/auth.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAGvC,wBAAsB,eAAe,CACnC,GAAG,EAAE,WAAW,EAChB,GAAG,EAAE,QAAQ,GACZ,OAAO,CAAC,IAAI,CAAC,CAWf;AAED,wBAAsB,YAAY,CAChC,GAAG,EAAE,WAAW,EAChB,GAAG,EAAE,QAAQ,GACZ,OAAO,CAAC,IAAI,CAAC,CAYf"}
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerHandler = registerHandler;
4
+ exports.loginHandler = loginHandler;
5
+ const auth_service_1 = require("../services/auth.service");
6
+ const errors_1 = require("../utils/errors");
7
+ async function registerHandler(req, res) {
8
+ try {
9
+ await (0, auth_service_1.registerUser)(req.body);
10
+ res.status(201).json({ message: "User registered successfully" });
11
+ }
12
+ catch (err) {
13
+ if (err instanceof errors_1.AuthError) {
14
+ res.status(err.statusCode).json({ message: err.message });
15
+ }
16
+ else if (err instanceof Error) {
17
+ res.status(400).json({ message: err.message });
18
+ }
19
+ }
20
+ }
21
+ async function loginHandler(req, res) {
22
+ try {
23
+ const { email, password } = req.body;
24
+ const tokens = await (0, auth_service_1.loginUser)(email, password);
25
+ res.status(200).json(tokens);
26
+ }
27
+ catch (err) {
28
+ if (err instanceof errors_1.AuthError) {
29
+ res.status(err.statusCode).json({ message: err.message });
30
+ }
31
+ else if (err instanceof Error) {
32
+ res.status(400).json({ message: err.message });
33
+ }
34
+ }
35
+ }
36
+ //# sourceMappingURL=auth.handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.handler.js","sourceRoot":"","sources":["../../src/handlers/auth.handler.ts"],"names":[],"mappings":";;AAKA,0CAcC;AAED,oCAeC;AAnCD,2DAAmE;AAEnE,4CAA4C;AAErC,KAAK,UAAU,eAAe,CACnC,GAAgB,EAChB,GAAa;IAEb,IAAI,CAAC;QACH,MAAM,IAAA,2BAAY,EAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC7B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,8BAA8B,EAAE,CAAC,CAAC;IACpE,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,kBAAS,EAAE,CAAC;YAC7B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;QAC5D,CAAC;aAAM,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;YAChC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;AACH,CAAC;AAEM,KAAK,UAAU,YAAY,CAChC,GAAgB,EAChB,GAAa;IAEb,IAAI,CAAC;QACH,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC;QACrC,MAAM,MAAM,GAAG,MAAM,IAAA,wBAAS,EAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAChD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,kBAAS,EAAE,CAAC;YAC7B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;QAC5D,CAAC;aAAM,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;YAChC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { registerHandler, loginHandler } from "./handlers/auth.handler";
2
+ import { protect } from "./middleware/protect";
3
+ import { AuthConfig, AuthAdapter, AuthUser, AuthRequest } from "./types";
4
+ export declare function init(config: AuthConfig): void;
5
+ export { registerHandler, loginHandler, protect };
6
+ export type { AuthConfig, AuthAdapter, AuthUser, AuthRequest };
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACxE,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEzE,wBAAgB,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAE7C;AAED,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC;AAGlD,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.protect = exports.loginHandler = exports.registerHandler = void 0;
4
+ exports.init = init;
5
+ const auth_config_1 = require("./config/auth.config");
6
+ const auth_handler_1 = require("./handlers/auth.handler");
7
+ Object.defineProperty(exports, "registerHandler", { enumerable: true, get: function () { return auth_handler_1.registerHandler; } });
8
+ Object.defineProperty(exports, "loginHandler", { enumerable: true, get: function () { return auth_handler_1.loginHandler; } });
9
+ const protect_1 = require("./middleware/protect");
10
+ Object.defineProperty(exports, "protect", { enumerable: true, get: function () { return protect_1.protect; } });
11
+ function init(config) {
12
+ (0, auth_config_1.initConfig)(config);
13
+ }
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAKA,oBAEC;AAPD,sDAAkD;AAClD,0DAAwE;AAQ/D,gGARA,8BAAe,OAQA;AAAE,6FARA,2BAAY,OAQA;AAPtC,kDAA+C;AAOP,wFAP/B,iBAAO,OAO+B;AAJ/C,SAAgB,IAAI,CAAC,MAAkB;IACrC,IAAA,wBAAU,EAAC,MAAM,CAAC,CAAC;AACrB,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { Response, NextFunction } from "express";
2
+ import { AuthRequest } from "../types";
3
+ export declare function protect(req: AuthRequest, res: Response, next: NextFunction): Promise<void>;
4
+ //# sourceMappingURL=protect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protect.d.ts","sourceRoot":"","sources":["../../src/middleware/protect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAGjD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAGvC,wBAAsB,OAAO,CAC3B,GAAG,EAAE,WAAW,EAChB,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC,IAAI,CAAC,CAyBf"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.protect = protect;
4
+ const jwt_service_1 = require("../services/jwt.service");
5
+ const auth_config_1 = require("../config/auth.config");
6
+ const errors_1 = require("../utils/errors");
7
+ async function protect(req, res, next) {
8
+ try {
9
+ const authHeader = req.headers.authorization;
10
+ if (!authHeader?.startsWith("Bearer ")) {
11
+ throw new errors_1.AuthError("No token provided", 401);
12
+ }
13
+ const token = authHeader.split(" ")[1];
14
+ const decoded = (0, jwt_service_1.verifyToken)(token);
15
+ const { adapter } = (0, auth_config_1.getConfig)();
16
+ const user = await adapter.findUserById(decoded.id);
17
+ if (!user)
18
+ throw new errors_1.AuthError("User no longer exists", 401);
19
+ req.user = user;
20
+ next();
21
+ }
22
+ catch (err) {
23
+ if (err instanceof errors_1.AuthError) {
24
+ res.status(err.statusCode).json({ message: err.message });
25
+ }
26
+ else {
27
+ res.status(401).json({ message: "Invalid or expired token" });
28
+ }
29
+ }
30
+ }
31
+ //# sourceMappingURL=protect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protect.js","sourceRoot":"","sources":["../../src/middleware/protect.ts"],"names":[],"mappings":";;AAMA,0BA6BC;AAlCD,yDAAsD;AACtD,uDAAkD;AAElD,4CAA4C;AAErC,KAAK,UAAU,OAAO,CAC3B,GAAgB,EAChB,GAAa,EACb,IAAkB;IAElB,IAAI,CAAC;QACH,MAAM,UAAU,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC;QAE7C,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;YACvC,MAAM,IAAI,kBAAS,CAAC,mBAAmB,EAAE,GAAG,CAAC,CAAC;QAChD,CAAC;QAED,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACvC,MAAM,OAAO,GAAG,IAAA,yBAAW,EAAC,KAAK,CAAC,CAAC;QAEnC,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,uBAAS,GAAE,CAAC;QAChC,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAEpD,IAAI,CAAC,IAAI;YAAE,MAAM,IAAI,kBAAS,CAAC,uBAAuB,EAAE,GAAG,CAAC,CAAC;QAE7D,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;QAChB,IAAI,EAAE,CAAC;IACT,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,kBAAS,EAAE,CAAC;YAC7B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;QAC5D,CAAC;aAAM,CAAC;YACN,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,0BAA0B,EAAE,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,10 @@
1
+ export declare function registerUser(userData: {
2
+ email: string;
3
+ password: string;
4
+ role?: string;
5
+ }): Promise<void>;
6
+ export declare function loginUser(email: string, password: string): Promise<{
7
+ accessToken: string;
8
+ refreshToken: string;
9
+ }>;
10
+ //# sourceMappingURL=auth.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.service.d.ts","sourceRoot":"","sources":["../../src/services/auth.service.ts"],"names":[],"mappings":"AAIA,wBAAsB,YAAY,CAAC,QAAQ,EAAE;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,GAAG,OAAO,CAAC,IAAI,CAAC,CAQhB;AAED,wBAAsB,SAAS,CAC7B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC,CAaxD"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerUser = registerUser;
4
+ exports.loginUser = loginUser;
5
+ const password_service_1 = require("./password.service");
6
+ const jwt_service_1 = require("./jwt.service");
7
+ const auth_config_1 = require("../config/auth.config");
8
+ async function registerUser(userData) {
9
+ const { adapter } = (0, auth_config_1.getConfig)();
10
+ const existing = await adapter.findUserByEmail(userData.email);
11
+ if (existing)
12
+ throw new Error("User already exists");
13
+ const hashed = await (0, password_service_1.hashPassword)(userData.password);
14
+ await adapter.createUser({ ...userData, password: hashed });
15
+ }
16
+ async function loginUser(email, password) {
17
+ const { adapter } = (0, auth_config_1.getConfig)();
18
+ const user = await adapter.findUserByEmail(email);
19
+ if (!user)
20
+ throw new Error("User not found");
21
+ const isValid = await (0, password_service_1.comparePassword)(password, user.password);
22
+ if (!isValid)
23
+ throw new Error("Invalid credentials");
24
+ const accessToken = (0, jwt_service_1.signAccessToken)(user);
25
+ const refreshToken = (0, jwt_service_1.signRefreshToken)(user);
26
+ return { accessToken, refreshToken };
27
+ }
28
+ //# sourceMappingURL=auth.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.service.js","sourceRoot":"","sources":["../../src/services/auth.service.ts"],"names":[],"mappings":";;AAIA,oCAYC;AAED,8BAgBC;AAlCD,yDAAmE;AACnE,+CAAkE;AAClE,uDAAkD;AAE3C,KAAK,UAAU,YAAY,CAAC,QAIlC;IACC,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,uBAAS,GAAE,CAAC;IAEhC,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,eAAe,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC/D,IAAI,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAErD,MAAM,MAAM,GAAG,MAAM,IAAA,+BAAY,EAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACrD,MAAM,OAAO,CAAC,UAAU,CAAC,EAAE,GAAG,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;AAC9D,CAAC;AAEM,KAAK,UAAU,SAAS,CAC7B,KAAa,EACb,QAAgB;IAEhB,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,uBAAS,GAAE,CAAC;IAEhC,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAClD,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAE7C,MAAM,OAAO,GAAG,MAAM,IAAA,kCAAe,EAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC/D,IAAI,CAAC,OAAO;QAAE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAErD,MAAM,WAAW,GAAG,IAAA,6BAAe,EAAC,IAAI,CAAC,CAAC;IAC1C,MAAM,YAAY,GAAG,IAAA,8BAAgB,EAAC,IAAI,CAAC,CAAC;IAE5C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,CAAC;AACvC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import jwt from "jsonwebtoken";
2
+ import { AuthUser } from "../types";
3
+ export declare function signAccessToken(user: AuthUser): string;
4
+ export declare function signRefreshToken(user: AuthUser): string;
5
+ export declare function verifyToken(token: string): jwt.JwtPayload;
6
+ //# sourceMappingURL=jwt.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt.service.d.ts","sourceRoot":"","sources":["../../src/services/jwt.service.ts"],"names":[],"mappings":"AAAA,OAAO,GAAoB,MAAM,cAAc,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGpC,wBAAgB,eAAe,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,CAItD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,CAIvD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,CAAC,UAAU,CAGzD"}
@@ -0,0 +1,25 @@
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.signAccessToken = signAccessToken;
7
+ exports.signRefreshToken = signRefreshToken;
8
+ exports.verifyToken = verifyToken;
9
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
10
+ const auth_config_1 = require("../config/auth.config");
11
+ function signAccessToken(user) {
12
+ const { secret, accessTokenExpiry } = (0, auth_config_1.getConfig)();
13
+ const options = { expiresIn: accessTokenExpiry };
14
+ return jsonwebtoken_1.default.sign({ id: user.id, email: user.email, role: user.role }, secret, options);
15
+ }
16
+ function signRefreshToken(user) {
17
+ const { secret, refreshTokenExpiry } = (0, auth_config_1.getConfig)();
18
+ const options = { expiresIn: refreshTokenExpiry };
19
+ return jsonwebtoken_1.default.sign({ id: user.id }, secret, options);
20
+ }
21
+ function verifyToken(token) {
22
+ const { secret } = (0, auth_config_1.getConfig)();
23
+ return jsonwebtoken_1.default.verify(token, secret);
24
+ }
25
+ //# sourceMappingURL=jwt.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt.service.js","sourceRoot":"","sources":["../../src/services/jwt.service.ts"],"names":[],"mappings":";;;;;AAKA,0CAIC;AAED,4CAIC;AAED,kCAGC;AApBD,gEAAgD;AAChD,uDAAkD;AAIlD,SAAgB,eAAe,CAAC,IAAc;IAC5C,MAAM,EAAE,MAAM,EAAE,iBAAiB,EAAE,GAAG,IAAA,uBAAS,GAAE,CAAC;IAClD,MAAM,OAAO,GAAgB,EAAE,SAAS,EAAE,iBAA6C,EAAE,CAAC;IAC1F,OAAO,sBAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AACxF,CAAC;AAED,SAAgB,gBAAgB,CAAC,IAAc;IAC7C,MAAM,EAAE,MAAM,EAAE,kBAAkB,EAAE,GAAG,IAAA,uBAAS,GAAE,CAAC;IACnD,MAAM,OAAO,GAAgB,EAAE,SAAS,EAAE,kBAA8C,EAAE,CAAC;IAC3F,OAAO,sBAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AACpD,CAAC;AAED,SAAgB,WAAW,CAAC,KAAa;IACvC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAA,uBAAS,GAAE,CAAC;IAC/B,OAAO,sBAAG,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAmB,CAAC;AACrD,CAAC"}
@@ -0,0 +1,3 @@
1
+ export declare function hashPassword(plain: string): Promise<string>;
2
+ export declare function comparePassword(plain: string, hashed: string): Promise<boolean>;
3
+ //# sourceMappingURL=password.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"password.service.d.ts","sourceRoot":"","sources":["../../src/services/password.service.ts"],"names":[],"mappings":"AAIA,wBAAsB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAEjE;AAED,wBAAsB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAErF"}
@@ -0,0 +1,16 @@
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.hashPassword = hashPassword;
7
+ exports.comparePassword = comparePassword;
8
+ const bcryptjs_1 = __importDefault(require("bcryptjs"));
9
+ const SALT_ROUNDS = 10;
10
+ async function hashPassword(plain) {
11
+ return bcryptjs_1.default.hash(plain, SALT_ROUNDS);
12
+ }
13
+ async function comparePassword(plain, hashed) {
14
+ return bcryptjs_1.default.compare(plain, hashed);
15
+ }
16
+ //# sourceMappingURL=password.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"password.service.js","sourceRoot":"","sources":["../../src/services/password.service.ts"],"names":[],"mappings":";;;;;AAIA,oCAEC;AAED,0CAEC;AAVD,wDAA8B;AAE9B,MAAM,WAAW,GAAG,EAAE,CAAC;AAEhB,KAAK,UAAU,YAAY,CAAC,KAAa;IAC9C,OAAO,kBAAM,CAAC,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;AACzC,CAAC;AAEM,KAAK,UAAU,eAAe,CAAC,KAAa,EAAE,MAAc;IACjE,OAAO,kBAAM,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACvC,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { Request } from "express";
2
+ export interface AuthUser {
3
+ id: string;
4
+ email: string;
5
+ password: string;
6
+ role?: string;
7
+ }
8
+ export interface AuthAdapter {
9
+ findUserByEmail: (email: string) => Promise<AuthUser | null>;
10
+ findUserById: (id: string) => Promise<AuthUser | null>;
11
+ createUser: (data: Partial<AuthUser>) => Promise<AuthUser>;
12
+ }
13
+ export interface AuthConfig {
14
+ secret: string;
15
+ accessTokenExpiry?: string;
16
+ refreshTokenExpiry?: string;
17
+ adapter: AuthAdapter;
18
+ }
19
+ export interface AuthRequest extends Request {
20
+ user?: AuthUser;
21
+ }
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAGlC,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,WAAW;IAC1B,eAAe,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;IAC7D,YAAY,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;IACvD,UAAU,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC5D;AAGD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,OAAO,EAAE,WAAW,CAAC;CACtB;AAGD,MAAM,WAAW,WAAY,SAAQ,OAAO;IAC1C,IAAI,CAAC,EAAE,QAAQ,CAAC;CACjB"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ export declare class AuthError extends Error {
2
+ statusCode: number;
3
+ constructor(message: string, statusCode?: number);
4
+ }
5
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,SAAU,SAAQ,KAAK;IAClC,UAAU,EAAE,MAAM,CAAC;gBAEP,OAAO,EAAE,MAAM,EAAE,UAAU,GAAE,MAAY;CAKtD"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthError = void 0;
4
+ class AuthError extends Error {
5
+ constructor(message, statusCode = 401) {
6
+ super(message);
7
+ this.name = "AuthError";
8
+ this.statusCode = statusCode;
9
+ }
10
+ }
11
+ exports.AuthError = AuthError;
12
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,SAAU,SAAQ,KAAK;IAGlC,YAAY,OAAe,EAAE,aAAqB,GAAG;QACnD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF;AARD,8BAQC"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@ilman00/authkit",
3
+ "version": "1.0.0",
4
+ "description": "Plug and play auth for Express apps",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "files": ["dist"],
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "dev": "tsc --watch"
11
+ },
12
+ "peerDependencies": {
13
+ "express": "^4.x || ^5.x"
14
+ },
15
+ "dependencies": {
16
+ "bcryptjs": "^2.4.3",
17
+ "jsonwebtoken": "^9.0.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/bcryptjs": "^2.4.6",
21
+ "@types/express": "^4.17.21",
22
+ "@types/jsonwebtoken": "^9.0.0",
23
+ "typescript": "^5.0.0"
24
+ },
25
+ "keywords": ["auth", "jwt", "express", "authentication"],
26
+ "license": "MIT"
27
+ }