Skip to content

Commit

Permalink
Merge pull request #64 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 20, 2024
2 parents 624cfca + aeed6a3 commit c3f94a8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
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.75",
"version": "1.1.76",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <[email protected]> (http://sikka.io)",
"license": "MIT",
Expand Down
15 changes: 7 additions & 8 deletions www/src/@sikka/hajar/core/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ async function login(email, password, config) {
throw new Error("Invalid user reference");
}

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

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

Expand Down Expand Up @@ -105,9 +105,8 @@ async function register(userDetails, config) {

const newAdmin = await admin.save();

const token = sign({ _id: newUser._id }, config.secret);
// i need to add the refresh token here
const refreshToken = sign({ _id: newUser._id }, config.refreshTokenSecret, {
const token = sign({ _id: newUser._id }, config.accessToken);
const refreshToken = sign({ _id: newUser._id }, config.refreshToken, {
expiresIn: "30d",
});

Expand All @@ -127,7 +126,7 @@ async function register(userDetails, config) {
async function getUserFromToken(accessToken, config) {
try {
const { models } = config.mongoose;
const decodedToken = verify(accessToken, config.secret);
const decodedToken = verify(accessToken, config.accessToken);
const user = await models.User.findById(decodedToken._id);
return user;
} catch (error) {
Expand All @@ -144,7 +143,7 @@ async function refreshAccessToken(refreshToken, config) {

let payload = {};
try {
payload = verify(refreshToken, config.refreshTokenSecret);
payload = verify(refreshToken, config.refreshToken);
} catch (err) {
throw new Error("Invalid token");
}
Expand All @@ -154,7 +153,7 @@ async function refreshAccessToken(refreshToken, config) {
throw new Error("User not found");
}

const newAccessToken = sign({ _id: user._id }, config.secret, {
const newAccessToken = sign({ _id: user._id }, config.accessToken, {
expiresIn: "1h",
});
return newAccessToken;
Expand Down
40 changes: 28 additions & 12 deletions www/src/@sikka/hajar/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ import {
} from "./auth/index.js";

class Hajar {
async init(url, secrets, mongooseInstance) {
if (this.initialized) {
throw new Error("🚫 Hajar is already initialized");
}
console.log("⌛ Starting the database connection process...");
try {
await mongooseInstance.connect(url);
mongooseInstance.connection.once("open", () => {
console.log("✅🔗 Connected to the database successfully!");
});
mongooseInstance.connection.on("error", (error) => {
console.error("❌🔗 Error connecting to the database:", error);
throw error;
});

this.config = {
accessToken: secrets.accessToken,
refreshToken: secrets.refreshToken,
mongoose: mongooseInstance,
};

this.initialized = true;
console.log("✅🚀 Hajar initialized successfully.");
} catch (error) {
console.error("❌🔗 Attempt to connect to the database failed: ", error);
throw error;
}
}
constructor() {
this.config = null;
this.initialized = false;
Expand Down Expand Up @@ -36,18 +64,6 @@ class Hajar {
},
};
}
initHajar(jwtSecret, refreshToken, mongooseInstance) {
if (this.initialized) {
throw new Error("Hajar is already initialized");
}
this.config = {
secret: jwtSecret,
refreshTokenSecret: refreshToken,
mongoose: mongooseInstance,
};
this.initialized = true;
console.log("Hajar initialized successfully.");
}
}

export default new Hajar();

0 comments on commit c3f94a8

Please sign in to comment.