Skip to content

Commit

Permalink
Merge pull request #61 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 939f0cb + b1502a8 commit 50f5218
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 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.71",
"version": "1.1.72",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <[email protected]> (http://sikka.io)",
"license": "MIT",
Expand Down
40 changes: 38 additions & 2 deletions www/src/@sikka/hajar/core/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { compare } from "bcrypt";
import { sign } from "jsonwebtoken";
import { sign, verify } from "jsonwebtoken";

async function login(config, email, password) {
const { models } = config.mongoose;
Expand Down Expand Up @@ -50,4 +50,40 @@ async function login(config, email, password) {
}
}

export { login };
async function getUserFromToken(accessToken, config) {
try {
const { models } = config.mongoose;
const decodedToken = verify(accessToken, config.secret);
const user = await models.User.findById(decodedToken._id);
return user;
} catch (error) {
console.log(error);
return null;
}
}

async function refreshAccessToken(refreshToken, config) {
if (!refreshToken) {
throw new Error("No token provided");
}
const { models } = config.mongoose;

let payload = {};
try {
payload = verify(refreshToken, config.refreshTokenSecret);
} catch (err) {
throw new Error("Invalid token");
}

const user = await models.User.findById(payload._id);
if (!user) {
throw new Error("User not found");
}

const newAccessToken = sign({ _id: user._id }, config.secret, {
expiresIn: "1h",
});
return newAccessToken;
}

export { login, getUserFromToken, refreshAccessToken };
14 changes: 13 additions & 1 deletion www/src/@sikka/hajar/core/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { login } from "./auth/index.js";
import { login, getUserFromToken, refreshAccessToken } from "./auth/index.js";

class Hajar {
constructor() {
Expand All @@ -11,6 +11,18 @@ class Hajar {
}
return login(this.config, email, password);
}.bind(this),
getUserFromToken: function (accessToken) {
if (!this.initialized) {
throw new Error("Hajar is not initialized");
}
return getUserFromToken(accessToken, this.config);
}.bind(this),
refreshAccessToken: function (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 50f5218

Please sign in to comment.