Skip to content

Commit

Permalink
Merge pull request #57 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 46434b2 + 0bf3657 commit 0729567
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 130 deletions.
113 changes: 2 additions & 111 deletions package-lock.json

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

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sikka/hajar",
"version": "1.1.68",
"version": "1.1.69",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <[email protected]> (http://sikka.io)",
"license": "MIT",
Expand Down Expand Up @@ -63,15 +63,12 @@
"dependencies": {
"@firebase/app": "~0.9.22",
"@firebase/auth": "~1.3.2",
"@graphql-tools/schema": "~10.0.0",
"@rollup/plugin-json": "~6.0.1",
"aws-sdk": "~2.1490.0",
"base64-stream": "~1.0.0",
"bcrypt": "~5.1.1",
"date-fns": "~2.30.0",
"dotenv": "~16.3.1",
"graphql": "~16.8.1",
"graphql-tag": "~2.12.6",
"handlebars": "~4.7.7",
"jsonwebtoken": "~9.0.2",
"moment": "~2.29.4",
Expand Down
6 changes: 3 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* import { version as _version } from "../www/src/@sikka/hajar/index";
*/
import { version as _version } from "../www/src/@sikka/hajar/index";

import { version } from "../package.json";
describe("Hajar.src.js", () => {
it("should get the library's version", () => {
expect(["1.1.68-beta", "1.1.68"]).toContain(version);
expect(["1.1.69-beta", "1.1.69"]).toContain(version);
});
});
13 changes: 10 additions & 3 deletions www/src/@sikka/hajar/core/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { compare } from "bcrypt";
import { sign } from "jsonwebtoken";

async function login(models, config, email, password) {
async function login(config, email, password) {
const { models } = config.mongoose;
const user = await models.User.findOne({ email });
if (!user) {
throw new Error("User not found");
Expand All @@ -12,8 +13,14 @@ async function login(models, config, email, password) {
throw new Error("Invalid password");
}

const token = sign({ _id: user._id }, config.secret);
return token;
const accessToken = sign({ _id: user._id }, config.secret, {
expiresIn: "7d",
});
const refreshToken = sign({ userId: user._id }, config.refreshTokenSecret, {
expiresIn: "7d",
});

return { accessToken, refreshToken };
}

export { login };
12 changes: 3 additions & 9 deletions www/src/@sikka/hajar/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,24 @@ 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);
return login(this.config, email, password);
}.bind(this),
};
}
initHajar(jwtSecret, mongooseInstance, userModel, adminModel, clientModel) {
initHajar(jwtSecret, refreshToken, mongooseInstance) {
if (this.initialized) {
throw new Error("Hajar is already initialized");
}

this.models = {
User: userModel,
Admin: adminModel,
Client: clientModel,
};
this.config = {
secret: jwtSecret,
refreshTokenSecret: refreshToken,
mongoose: mongooseInstance,
};
this.initialized = true;
Expand Down

0 comments on commit 0729567

Please sign in to comment.