Skip to content

Commit

Permalink
Merge pull request #65 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 23, 2024
2 parents c3f94a8 + 663290f commit dfe6544
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 49 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.76",
"version": "1.1.78",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <[email protected]> (http://sikka.io)",
"license": "MIT",
Expand Down
94 changes: 50 additions & 44 deletions www/src/@sikka/hajar/core/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
import { compare, hash } from "bcrypt";
import { sign, verify } from "jsonwebtoken";
import HajarError from "../utils/hajarError";

async function login(email, password, config) {
const { models } = config.mongoose;
const user = await models.User.findOne({ email });
if (!user) {
throw new Error("User not found");
}
async function login(email, password, userType, config) {
try {
const { models } = config.mongoose;
const user = await models.User.findOne({ email });
if (!user) {
throw new HajarError("User not found", "invalid-email-password");
}

const validPassword = await compare(password, user.password);
if (!validPassword) {
throw new Error("Invalid password");
}
const validPassword = await compare(password, user.password);

const ref = user.ref;
let additionalData = null;

switch (ref) {
case "admin":
additionalData = await models.Admin.findOne({ uid: user._id });
if (!additionalData) {
throw new Error("Admin not found");
}
break;
case "client":
additionalData = await models.Client.findOne({ uid: user._id });
if (!additionalData) {
throw new Error("Client not found");
}
break;
default:
throw new Error("Invalid user reference");
}
if (!validPassword) {
throw new HajarError("Invalid password", "invalid-email-password");
}

const token = sign({ _id: user._id }, config.accessToken, {
expiresIn: "7d",
});
// Capitalize the first letter of userType
const modelType = userType.charAt(0).toUpperCase() + userType.slice(1);

const refreshToken = sign({ _id: user._id }, config.refreshToken, {
expiresIn: "30d",
});
// Check if the model exists in models on our mongoose instance
if (!models[modelType]) {
throw new HajarError("Invalid user type", "invalid_user_type");
}

return {
success: true,
user: { ...user.toObject() },
[ref]: { ...additionalData.toObject() },
token,
refreshToken,
};
const additionalData = await models[modelType].findOne({ uid: user._id });
if (!additionalData) {
throw new HajarError(`${modelType} not found`, `${userType}_not_found`);
}

const token = sign({ _id: user._id }, config.accessToken, {
expiresIn: "1h",
});

const refreshToken = sign({ _id: user._id }, config.refreshToken, {
expiresIn: "7d",
});

return {
success: true,
user: { ...user.toObject() },
[userType]: { ...additionalData.toObject() },
token,
refreshToken,
};
} catch (error) {
console.error("Login error:", error);
throw error;
}
}

// @TODO: Add the ability to register a client in the same function
Expand All @@ -63,10 +63,16 @@ async function register(userDetails, config) {
});

if (usernameCheck) {
throw new Error("User with this username already exists");
throw new HajarError(
"User with this username already exists",
"username_exists"
);
}
if (userExists) {
throw new Error("User with this email already exists");
throw new HajarError(
"User with this email already exists",
"email_exists"
);
}

const adminRole = await models.Role.findOne({
Expand Down
4 changes: 2 additions & 2 deletions www/src/@sikka/hajar/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class Hajar {
this.config = null;
this.initialized = false;
this.auth = {
login: (email, password) => {
login: (email, password, userType) => {
if (!this.initialized) {
throw new Error("Hajar is not initialized");
}
return login(email, password, this.config);
return login(email, password, userType, this.config);
},
register: (userDetails) => {
if (!this.initialized) {
Expand Down
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); // Pass the message parameter to the Error constructor
this.slug = slug; // Assign the code parameter to a property on the error object
if (customProperties) {
Object.assign(this, customProperties);
}
}
}

export default HajarError;

0 comments on commit dfe6544

Please sign in to comment.