Skip to content

Commit

Permalink
chore : update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansourkira committed Mar 19, 2024
1 parent 749b06a commit 7940578
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 62 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sikka/hajar",
"version": "1.1.65",
"version": "1.1.66",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <contact@sikka.io> (http://sikka.io)",
"license": "MIT",
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.65-beta", "1.1.65"]).toContain(version);
expect(["1.1.66-beta", "1.1.66"]).toContain(version);
});
});
16 changes: 13 additions & 3 deletions www/src/@sikka/hajar/core/auth/login.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { sign } from "jsonwebtoken";
import { compare } from "bcrypt";
import * as initHajarModule from "../init.js";
const { User, secret, getClientData, getAdminData, getUserType } =
initHajarModule;

async function login(email, password) {
const {
User,
getClientData,
getAdminData,
getUserType,
jwtSecret,
// mongoose,
} = initHajarModule;
const { secret } = initHajarModule;

const user = await User.findOne({ email });

if (!user) {
Expand All @@ -15,7 +23,9 @@ async function login(email, password) {
throw new Error("Invalid email or password");
}

const token = sign({ userId: user._id }, secret, { expiresIn: "1h" });
const token = sign({ userId: user._id }, jwtSecret || secret, {
expiresIn: "1h",
});

const userData = {
success: true,
Expand Down
119 changes: 66 additions & 53 deletions www/src/@sikka/hajar/core/init.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,80 @@
let initialized = false;
let secret;
let mongooseInstance;
let User;
let Admin;
let Client;

function initHajar(
jwtSecret,
inputMongooseInstance,
mongooseInstance,
userModel,
adminModel,
clientModel
) {
if (initialized) {
throw new Error("Hajar is already initialized");
}
let initialized = false;

secret = jwtSecret;
mongooseInstance = inputMongooseInstance;
User = userModel;
Admin = adminModel;
Client = clientModel;
initialized = true;
}
let User = userModel;
let Admin = adminModel;
let Client = clientModel;
let secret = jwtSecret;
let mongoose = mongooseInstance;

async function getUserType(email) {
const user = await User.findOne({ email });
if (!user) {
throw new Error("User not found");
}
return user.ref;
}
const initialize = () => {
if (initialized) {
throw new Error("Hajar is already initialized");
}

initialized = true;
console.log("Hajar initialized successfully.");
};

async function getAdminData(user) {
if (user.ref === "admin") {
const adminData = await Admin.findOne({ uid: user._id });
if (!adminData) {
throw new Error("Admin data not found");
const getUserType = async (email) => {
if (!initialized) {
throw new Error("Hajar is not initialized");
}
return adminData;
}
return null;
}
const user = await User.findOne({ email });
if (!user) {
throw new Error("User not found");
}
return user.ref;
};

async function getClientData(user) {
if (user.ref === "client") {
const clientData = await Client.findOne({ uid: user._id });
if (!clientData) {
throw new Error("Client data not found");
const getAdminData = async (user) => {
if (!initialized) {
throw new Error("Hajar is not initialized");
}
return clientData;
}
return null;
if (user.ref === "admin") {
const adminData = await Admin.findOne({ uid: user._id });
if (!adminData) {
throw new Error("Admin data not found");
}
return adminData;
}
return null;
};

const getClientData = async (user) => {
if (!initialized) {
throw new Error("Hajar is not initialized");
}
if (user.ref === "client") {
const clientData = await Client.findOne({ uid: user._id });
if (!clientData) {
throw new Error("Client data not found");
}
return clientData;
}
return null;
};

// Initialize hajar with external data
initialize();

// Expose functions and models
return {
getUserType,
getAdminData,
getClientData,
User, // Expose User model
Admin, // Expose Admin model
Client, // Expose Client model
secret, // Expose secret
mongoose, // Expose mongoose
};
}

export {
initHajar,
getUserType,
getAdminData,
getClientData,
secret,
mongooseInstance,
User,
Admin,
Client,
};
export { initHajar };
4 changes: 2 additions & 2 deletions www/src/@sikka/hajar/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as initHajarModule from "./core/init.js";
import { initHajar } from "./core/init.js";
import auth from "./core/auth/index.js";
import { version as _version } from "../../../../package.json";

const hajar = {
...initHajarModule,
initHajar: initHajar,
auth,
version: _version,
};
Expand Down

0 comments on commit 7940578

Please sign in to comment.