Skip to content

Commit

Permalink
Merge pull request #62 from sikka-software/feature/enhance-hajar-usab…
Browse files Browse the repository at this point in the history
…ility-js

Add register to  Hajar.auth module
  • Loading branch information
Mansourkira committed Mar 20, 2024
2 parents 50f5218 + 133fd73 commit 88dbb3d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 36 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.72",
"version": "1.1.74",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <contact@sikka.io> (http://sikka.io)",
"license": "MIT",
Expand Down
118 changes: 93 additions & 25 deletions www/src/@sikka/hajar/core/auth/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -86,4 +154,4 @@ async function refreshAccessToken(refreshToken, config) {
return newAccessToken;
}

export { login, getUserFromToken, refreshAccessToken };
export { login, register, getUserFromToken, refreshAccessToken };
27 changes: 19 additions & 8 deletions www/src/@sikka/hajar/core/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 88dbb3d

Please sign in to comment.