Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/enhance hajar usability #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"scripts": {
"build": "rollup -c --environment BUILD:production",
"dev": "rollup -c -w",
"lint": "eslint --ext .js,.ts src/",
"lint": "eslint --ext .js,.ts www/src/",
"test": "jest --config jest.config.js",
"test:watch": "npm run test -- --watch",
"test:coverage": "npm run test -- --coverage",
Expand Down
4 changes: 4 additions & 0 deletions www/src/@sikka/hajar/core/auth/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const login = require("./login");
module.exports = {
login,
};
42 changes: 42 additions & 0 deletions www/src/@sikka/hajar/core/auth/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const {
User,
secret,
getClientData,
getAdminData,
getUserType,
} = require("../init.js");

async function login(email, password) {
const user = await User.findOne({ email });

if (!user) {
throw new Error("Invalid email or password");
}

if (!(await bcrypt.compare(password, user.password))) {
throw new Error("Invalid email or password");
}

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

const userData = {
success: true,
user: user.toObject(),
token,
};

switch (await getUserType(email)) {
case "admin":
userData.admin = await getAdminData(user);
break;
case "client":
userData.client = await getClientData(user);
break;
}

return userData;
}

module.exports = login;
14 changes: 14 additions & 0 deletions www/src/@sikka/hajar/core/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ export async function getClientData(user: any): Promise<any> {
return null;
}

<<<<<<< Updated upstream:www/src/@sikka/hajar/core/init.ts
export { secret, mongooseInstance, User, Admin, Client };
=======
module.exports = {
initHajar,
getUserType,
getAdminData,
getClientData,
secret,
mongooseInstance,
User,
Admin,
Client,
};
>>>>>>> Stashed changes:www/src/@sikka/hajar/core/init.js
11 changes: 11 additions & 0 deletions www/src/@sikka/hajar/core/utils/HajarError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class HajarError extends Error {
constructor(message, slug, customProperties) {
super(message);
this.slug = slug;
if (customProperties) {
Object.assign(this, customProperties);
}
}
}

module.exports = HajarError;
9 changes: 9 additions & 0 deletions www/src/@sikka/hajar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { initHajar } = require("./core/init.js");
const auth = require("./core/auth/index.js");
const hajar = {
initHajar,
auth,
version: "1.0.41",
};

module.exports = hajar;