Skip to content

Commit

Permalink
Merge pull request #56 from sikka-software/feature/enhance-hajar-usab…
Browse files Browse the repository at this point in the history
…ility-js

Feature/enhance hajar usability js
  • Loading branch information
Mansourkira authored Mar 20, 2024
2 parents 1b9f83f + 4b0bdfb commit 46434b2
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 93 deletions.
18 changes: 10 additions & 8 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -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"],
};
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"es6": true,
"node": true
},
"parser": "@babel/eslint-parser",
"extends": ["eslint:recommended"],
"rules": {}
}
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
19 changes: 19 additions & 0 deletions www/src/@sikka/hajar/core/auth/index.js
Original file line number Diff line number Diff line change
@@ -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 };
25 changes: 0 additions & 25 deletions www/src/@sikka/hajar/core/auth/index.ts

This file was deleted.

37 changes: 37 additions & 0 deletions www/src/@sikka/hajar/core/index.js
Original file line number Diff line number Diff line change
@@ -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();
57 changes: 0 additions & 57 deletions www/src/@sikka/hajar/core/index.ts

This file was deleted.

0 comments on commit 46434b2

Please sign in to comment.