From e593b2d19b640a987225b26b08f2534f668b6d7c Mon Sep 17 00:00:00 2001 From: mansourkira Date: Thu, 21 Mar 2024 21:16:42 +0100 Subject: [PATCH 1/6] chore : add Hajar Error in auth module --- www/src/@sikka/hajar/core/auth/index.js | 22 +++++++++++++------ www/src/@sikka/hajar/core/utils/hajarError.js | 11 ++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 www/src/@sikka/hajar/core/utils/hajarError.js diff --git a/www/src/@sikka/hajar/core/auth/index.js b/www/src/@sikka/hajar/core/auth/index.js index 6a48375..21866c3 100644 --- a/www/src/@sikka/hajar/core/auth/index.js +++ b/www/src/@sikka/hajar/core/auth/index.js @@ -1,16 +1,17 @@ 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"); + throw new HajarError("User not found", "invalid-email-password"); } const validPassword = await compare(password, user.password); if (!validPassword) { - throw new Error("Invalid password"); + throw new HajarError("Invalid password", "invalid-email-password"); } const ref = user.ref; @@ -20,17 +21,17 @@ async function login(email, password, config) { case "admin": additionalData = await models.Admin.findOne({ uid: user._id }); if (!additionalData) { - throw new Error("Admin not found"); + throw new HajarError("Admin not found", "admin_not_found"); } break; case "client": additionalData = await models.Client.findOne({ uid: user._id }); if (!additionalData) { - throw new Error("Client not found"); + throw new HajarError("Client not found", "client_not_found"); } break; default: - throw new Error("Invalid user reference"); + throw new HajarError("Invalid user reference", "invalid_user_reference"); } const token = sign({ _id: user._id }, config.accessToken, { @@ -51,6 +52,7 @@ async function login(email, password, config) { } // @TODO: Add the ability to register a client in the same function + async function register(userDetails, config) { try { const { models } = config.mongoose; @@ -63,10 +65,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({ diff --git a/www/src/@sikka/hajar/core/utils/hajarError.js b/www/src/@sikka/hajar/core/utils/hajarError.js new file mode 100644 index 0000000..011f371 --- /dev/null +++ b/www/src/@sikka/hajar/core/utils/hajarError.js @@ -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); + } + } +} + +module.exports = HajarError; From f3be270ddffada9fd8b420fe6601374a33050c33 Mon Sep 17 00:00:00 2001 From: mansourkira Date: Thu, 21 Mar 2024 21:16:52 +0100 Subject: [PATCH 2/6] test in sikka : publish package --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 046dd3d..5e417a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sikka/hajar", - "version": "1.1.76", + "version": "1.1.77", "description": "Toolkit to create SaaS applications", "author": "Sikka Software (http://sikka.io)", "license": "MIT", From 4e2bd9234d2ae5ccb17d187e72ab080fbd45e9ec Mon Sep 17 00:00:00 2001 From: mansourkira Date: Thu, 21 Mar 2024 21:19:20 +0100 Subject: [PATCH 3/6] fix build : update hajarError class --- package-lock.json | 4 ++-- www/src/@sikka/hajar/core/utils/hajarError.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 583394a..f69b373 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sikka/hajar", - "version": "1.1.74", + "version": "1.1.77", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sikka/hajar", - "version": "1.1.74", + "version": "1.1.77", "license": "MIT", "dependencies": { "@firebase/app": "~0.9.22", diff --git a/www/src/@sikka/hajar/core/utils/hajarError.js b/www/src/@sikka/hajar/core/utils/hajarError.js index 011f371..a284df6 100644 --- a/www/src/@sikka/hajar/core/utils/hajarError.js +++ b/www/src/@sikka/hajar/core/utils/hajarError.js @@ -8,4 +8,4 @@ class HajarError extends Error { } } -module.exports = HajarError; +export default HajarError; From 02046060f618a10d2b923c59d726275344647c7c Mon Sep 17 00:00:00 2001 From: mansourkira Date: Sat, 23 Mar 2024 14:47:43 +0100 Subject: [PATCH 4/6] chore : update login to have new param ( userType ) --- www/src/@sikka/hajar/core/auth/index.js | 84 ++++++++++++------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/www/src/@sikka/hajar/core/auth/index.js b/www/src/@sikka/hajar/core/auth/index.js index 21866c3..512c5cd 100644 --- a/www/src/@sikka/hajar/core/auth/index.js +++ b/www/src/@sikka/hajar/core/auth/index.js @@ -2,57 +2,55 @@ 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 HajarError("User not found", "invalid-email-password"); - } +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 HajarError("Invalid password", "invalid-email-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 HajarError("Admin not found", "admin_not_found"); - } - break; - case "client": - additionalData = await models.Client.findOne({ uid: user._id }); - if (!additionalData) { - throw new HajarError("Client not found", "client_not_found"); - } - break; - default: - throw new HajarError("Invalid user reference", "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"); + } + + const additionalData = await models[modelType].findOne({ uid: user._id }); + if (!additionalData) { + throw new HajarError(`${modelType} not found`, `${userType}_not_found`); + } - return { - success: true, - user: { ...user.toObject() }, - [ref]: { ...additionalData.toObject() }, - token, - refreshToken, - }; + 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 - async function register(userDetails, config) { try { const { models } = config.mongoose; From add3652474a8101d3dce9f809a716a84e5fe1a3a Mon Sep 17 00:00:00 2001 From: mansourkira Date: Sat, 23 Mar 2024 14:47:58 +0100 Subject: [PATCH 5/6] chore : update login constructor --- www/src/@sikka/hajar/core/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/www/src/@sikka/hajar/core/index.js b/www/src/@sikka/hajar/core/index.js index 394bc17..5c8f1fc 100644 --- a/www/src/@sikka/hajar/core/index.js +++ b/www/src/@sikka/hajar/core/index.js @@ -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) { From 663290f369a1d5d478804865b8575eda321f65e9 Mon Sep 17 00:00:00 2001 From: mansourkira Date: Sat, 23 Mar 2024 14:48:09 +0100 Subject: [PATCH 6/6] test in sikka : publish package --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index f69b373..1896cf4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sikka/hajar", - "version": "1.1.77", + "version": "1.1.78", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sikka/hajar", - "version": "1.1.77", + "version": "1.1.78", "license": "MIT", "dependencies": { "@firebase/app": "~0.9.22", diff --git a/package.json b/package.json index 5e417a3..507cfcb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sikka/hajar", - "version": "1.1.77", + "version": "1.1.78", "description": "Toolkit to create SaaS applications", "author": "Sikka Software (http://sikka.io)", "license": "MIT",