From 9bdbd84204ba4e123b05dbdf1679015357d86670 Mon Sep 17 00:00:00 2001 From: mansourkira Date: Wed, 20 Mar 2024 21:35:56 +0100 Subject: [PATCH 1/3] chore : add register function --- www/src/@sikka/hajar/core/auth/index.js | 118 +++++++++++++++++++----- 1 file changed, 93 insertions(+), 25 deletions(-) diff --git a/www/src/@sikka/hajar/core/auth/index.js b/www/src/@sikka/hajar/core/auth/index.js index 1fe5199..896de07 100644 --- a/www/src/@sikka/hajar/core/auth/index.js +++ b/www/src/@sikka/hajar/core/auth/index.js @@ -1,7 +1,7 @@ -import { compare } from "bcrypt"; +import { compare, hash } from "bcrypt"; import { sign, verify } from "jsonwebtoken"; -async function login(config, email, password) { +async function login(email, password, config) { const { models } = config.mongoose; const user = await models.User.findOne({ email }); if (!user) { @@ -14,39 +14,107 @@ async function login(config, email, password) { } const ref = user.ref; - let adminData = null; - let clientData = null; + let additionalData = null; - if (ref === "admin") { - adminData = await models.Admin.findOne({ uid: user._id }); - if (!adminData) { - throw new Error("Admin not found"); - } - } else if (ref === "client") { - clientData = await models.Client.findOne({ uid: user._id }); - if (!clientData) { - throw new Error("Client not found"); - } + 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"); } const token = sign({ _id: user._id }, config.secret, { expiresIn: "7d", }); - if (ref === "admin") { - return { - success: true, - user: { ...user.toObject() }, - admin: { ...adminData.toObject() }, - token, - }; - } else if (ref === "client") { + const refreshToken = sign({ _id: user._id }, config.refreshTokenSecret, { + expiresIn: "30d", + }); + + return { + success: true, + user: { ...user.toObject() }, + [ref]: { ...additionalData.toObject() }, + token, + refreshToken, + }; +} + +async function register(userDetails, config) { + try { + const { models } = config.mongoose; + userDetails.email = userDetails.email.toLowerCase(); + const userExists = await models.User.findOne({ + email: userDetails.email, + }); + const usernameCheck = await models.User.findOne({ + username: userDetails.username, + }); + + if (usernameCheck) { + throw new Error("User with this username already exists"); + } + if (userExists) { + throw new Error("User with this email already exists"); + } + + const adminRole = await models.Role.findOne({ + name: "Admin", + }); + + if (!adminRole) { + const allPermissions = await models.Permission.find({}); + const newAdminRole = new models.Role({ + name: "Admin", + permissions: allPermissions, + }); + await newAdminRole.save(); + } + + const hashedPassword = await hash(userDetails.password, 10); + + const user = new models.User({ + username: userDetails.username, + email: userDetails.email, + ref: "admin", + password: hashedPassword, + role: adminRole._id, + }); + + const newUser = await user.save(); + + const admin = new config.mongoose.models.Admin({ + profile: newUser._id, + role: adminRole._id, + uid: newUser._id, + username: userDetails.username, + firstName: { en: "", ar: "" }, + lastName: { en: "", ar: "" }, + }); + + const newAdmin = await admin.save(); + + const token = sign({ _id: newUser._id }, config.secret); + return { success: true, - user: { ...user.toObject() }, - client: { ...clientData.toObject() }, + user: { ...newUser.toObject() }, + admin: { ...newAdmin.toObject() }, token, }; + } catch (error) { + console.error("Registration error:", error); + throw error; } } @@ -86,4 +154,4 @@ async function refreshAccessToken(refreshToken, config) { return newAccessToken; } -export { login, getUserFromToken, refreshAccessToken }; +export { login, register, getUserFromToken, refreshAccessToken }; From 09875de8a491121e0085dcc05d246310c587bd5f Mon Sep 17 00:00:00 2001 From: mansourkira Date: Wed, 20 Mar 2024 21:36:12 +0100 Subject: [PATCH 2/3] chore : clean up constructor & add register to Hajar --- www/src/@sikka/hajar/core/index.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/www/src/@sikka/hajar/core/index.js b/www/src/@sikka/hajar/core/index.js index f75c06f..24f5aab 100644 --- a/www/src/@sikka/hajar/core/index.js +++ b/www/src/@sikka/hajar/core/index.js @@ -1,28 +1,39 @@ -import { login, getUserFromToken, refreshAccessToken } from "./auth/index.js"; +import { + login, + register, + getUserFromToken, + refreshAccessToken, +} from "./auth/index.js"; class Hajar { constructor() { this.config = null; this.initialized = false; this.auth = { - login: function (email, password) { + login: (email, password) => { if (!this.initialized) { throw new Error("Hajar is not initialized"); } - return login(this.config, email, password); - }.bind(this), - getUserFromToken: function (accessToken) { + return login(email, password, this.config); + }, + register: (userDetails) => { + if (!this.initialized) { + throw new Error("Hajar is not initialized"); + } + return register(userDetails, this.config); + }, + getUserFromToken: (accessToken) => { if (!this.initialized) { throw new Error("Hajar is not initialized"); } return getUserFromToken(accessToken, this.config); - }.bind(this), - refreshAccessToken: function (refreshToken) { + }, + refreshAccessToken: (refreshToken) => { if (!this.initialized) { throw new Error("Hajar is not initialized"); } return refreshAccessToken(refreshToken, this.config); - }.bind(this), + }, }; } initHajar(jwtSecret, refreshToken, mongooseInstance) { From 133fd732bb4a9923256899fff12ce842193d58b5 Mon Sep 17 00:00:00 2001 From: mansourkira Date: Wed, 20 Mar 2024 21:37:40 +0100 Subject: [PATCH 3/3] test in sikka (backend ) : publish package SIKKA-6593[in progress] SIKKA-6618[in progress] --- 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 4fe0ae6..583394a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sikka/hajar", - "version": "1.1.71", + "version": "1.1.74", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sikka/hajar", - "version": "1.1.71", + "version": "1.1.74", "license": "MIT", "dependencies": { "@firebase/app": "~0.9.22", diff --git a/package.json b/package.json index 2c40357..c3a46c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sikka/hajar", - "version": "1.1.72", + "version": "1.1.74", "description": "Toolkit to create SaaS applications", "author": "Sikka Software (http://sikka.io)", "license": "MIT",