diff --git a/.babelrc.js b/.babelrc.js index 0295c6c..305c020 100644 --- a/.babelrc.js +++ b/.babelrc.js @@ -1,11 +1,13 @@ const { NODE_ENV } = process.env; -export const presets = [ - [ - "@babel/preset-env", - { - modules: NODE_ENV === "test" ? "auto" : false, - }, +module.exports = { + presets: [ + [ + "@babel/preset-env", + { + modules: NODE_ENV === "test" ? "auto" : false, + }, + ], ], -]; -export const plugins = ["@babel/plugin-proposal-object-rest-spread"]; + plugins: ["@babel/plugin-proposal-object-rest-spread"], +}; diff --git a/.eslintrc b/.eslintrc index adea449..4cb292a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -7,7 +7,6 @@ "es6": true, "node": true }, - "parser": "@babel/eslint-parser", "extends": ["eslint:recommended"], "rules": {} } diff --git a/package-lock.json b/package-lock.json index d2cf158..e41f816 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,6 +40,8 @@ "@rollup/plugin-commonjs": "~25.0.7", "@rollup/plugin-node-resolve": "~15.2.3", "@rollup/plugin-replace": "~5.0.5", + "@types/bcrypt": "~5.0.2", + "@types/jsonwebtoken": "~9.0.6", "@wwa/rollup-plugin-terser": "~1.1.2", "cross-env": "~7.0.3", "eslint": "~8.57.0", @@ -3594,6 +3596,15 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/bcrypt": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@types/bcrypt/-/bcrypt-5.0.2.tgz", + "integrity": "sha512-6atioO8Y75fNcbmj0G7UjI9lXN2pQ/IGJ2FWT4a/btd0Lk9lQalHLKhkgKVZ3r+spnmWUKfbMi1GEe9wyHQfNQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", @@ -3632,6 +3643,15 @@ "@types/istanbul-lib-report": "*" } }, + "node_modules/@types/jsonwebtoken": { + "version": "9.0.6", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.6.tgz", + "integrity": "sha512-/5hndP5dCjloafCXns6SZyESp3Ldq7YjH3zwzwczYnjxIT0Fqzk5ROSYVGfFyczIue7IUEj8hkvLbPoLQ18vQw==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/node": { "version": "20.11.29", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.29.tgz", diff --git a/package.json b/package.json index 39a863f..287d849 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,8 @@ "@rollup/plugin-commonjs": "~25.0.7", "@rollup/plugin-node-resolve": "~15.2.3", "@rollup/plugin-replace": "~5.0.5", + "@types/bcrypt": "~5.0.2", + "@types/jsonwebtoken": "~9.0.6", "@wwa/rollup-plugin-terser": "~1.1.2", "cross-env": "~7.0.3", "eslint": "~8.57.0", diff --git a/rollup.config.js b/rollup.config.js index 3a080ed..926bb60 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -21,7 +21,7 @@ const makeConfig = (env = "development") => { let bundleSuffix = env === "production" ? "min." : ""; return { - input: "./www/src/@sikka/hajar/core/index.ts", + input: "./www/src/@sikka/hajar/core/index.js", external: EXTERNAL, output: [ { diff --git a/test/index.spec.js b/test/index.spec.js index 0ae7816..e208e91 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -3,6 +3,6 @@ import { version } from "../package.json"; describe("Hajar.src.js", () => { it("should get the library's version", () => { - expect(["1.1.67-beta", "1.1.67"]).toContain(version); + expect(["1.1.68-beta", "1.1.68"]).toContain(version); }); }); diff --git a/www/src/@sikka/hajar/core/auth/index.js b/www/src/@sikka/hajar/core/auth/index.js new file mode 100644 index 0000000..d597dc5 --- /dev/null +++ b/www/src/@sikka/hajar/core/auth/index.js @@ -0,0 +1,19 @@ +import { compare } from "bcrypt"; +import { sign } from "jsonwebtoken"; + +async function login(models, config, email, password) { + const user = await models.User.findOne({ email }); + if (!user) { + throw new Error("User not found"); + } + + const validPassword = await compare(password, user.password); + if (!validPassword) { + throw new Error("Invalid password"); + } + + const token = sign({ _id: user._id }, config.secret); + return token; +} + +export { login }; diff --git a/www/src/@sikka/hajar/core/auth/index.ts b/www/src/@sikka/hajar/core/auth/index.ts deleted file mode 100644 index a2e4022..0000000 --- a/www/src/@sikka/hajar/core/auth/index.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Models } from "../index"; // Adjust the path as needed -import bcrypt from "bcrypt"; -import jwt from "jsonwebtoken"; -import { Config } from "../index"; // Assuming Config type is also exported from index.ts - -// @ts-ignore -export async function login( - models: Models, - config: Config, - email: string, - password: string -): Promise { - const user: any = await models.User.findOne({ email }); - if (!user) { - throw new Error("User not found"); - } - - const validPassword = await bcrypt.compare(password, user.password); - if (!validPassword) { - throw new Error("Invalid password"); - } - - const token = jwt.sign({ _id: user._id }, config.secret); - return token; -} diff --git a/www/src/@sikka/hajar/core/index.js b/www/src/@sikka/hajar/core/index.js new file mode 100644 index 0000000..19cc153 --- /dev/null +++ b/www/src/@sikka/hajar/core/index.js @@ -0,0 +1,37 @@ +import { login } from "./auth/index.js"; + +class Hajar { + constructor() { + this.models = null; + this.config = null; + this.initialized = false; + this.auth = { + login: function (email, password) { + if (!this.initialized) { + throw new Error("Hajar is not initialized"); + } + return login(this.models, this.config, email, password); + }.bind(this), + }; + } + initHajar(jwtSecret, mongooseInstance, userModel, adminModel, clientModel) { + if (this.initialized) { + throw new Error("Hajar is already initialized"); + } + + this.models = { + User: userModel, + Admin: adminModel, + Client: clientModel, + }; + this.config = { + secret: jwtSecret, + mongoose: mongooseInstance, + }; + this.initialized = true; + + console.log("Hajar initialized successfully."); + } +} + +export default new Hajar(); diff --git a/www/src/@sikka/hajar/core/index.ts b/www/src/@sikka/hajar/core/index.ts deleted file mode 100644 index fab947a..0000000 --- a/www/src/@sikka/hajar/core/index.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { login } from "./auth"; -import { Model, Document } from "mongoose"; - -export type Models = { - User: Model>; - Admin: Model>; - Client: Model>; -}; - -export type Config = { - secret: string; - mongoose: any; -}; - -class Hajar { - models!: Models; - config!: Config; - initialized: boolean; - auth: { - login: (email: string, password: string) => Promise; - }; - - constructor() { - this.initialized = false; - this.auth = { - login: (email: string, password: string) => - login(this.models, this.config, email, password), - }; - } - - initHajar( - jwtSecret: string, - mongooseInstance: any, - userModel: Model>, - adminModel: Model>, - clientModel: Model> - ) { - if (this.initialized) { - throw new Error("Hajar is already initialized"); - } - - this.models = { - User: userModel, - Admin: adminModel, - Client: clientModel, - }; - this.config = { - secret: jwtSecret, - mongoose: mongooseInstance, - }; - this.initialized = true; - - console.log("Hajar initialized successfully."); - } -} - -export default new Hajar();