diff --git a/package.json b/package.json index db6fee7..762a530 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "scripts": { "build": "rollup -c --environment BUILD:production", "dev": "rollup -c -w", - "lint": "eslint --ext .js,.ts src/", + "lint": "eslint --ext .js,.ts www/src/", "test": "jest --config jest.config.js", "test:watch": "npm run test -- --watch", "test:coverage": "npm run test -- --coverage", diff --git a/www/src/@sikka/hajar/core/auth/index.js b/www/src/@sikka/hajar/core/auth/index.js new file mode 100644 index 0000000..222e782 --- /dev/null +++ b/www/src/@sikka/hajar/core/auth/index.js @@ -0,0 +1,4 @@ +const login = require("./login"); +module.exports = { + login, +}; diff --git a/www/src/@sikka/hajar/core/auth/login.js b/www/src/@sikka/hajar/core/auth/login.js new file mode 100644 index 0000000..f6d1562 --- /dev/null +++ b/www/src/@sikka/hajar/core/auth/login.js @@ -0,0 +1,42 @@ +const jwt = require("jsonwebtoken"); +const bcrypt = require("bcrypt"); +const { + User, + secret, + getClientData, + getAdminData, + getUserType, +} = require("../init.js"); + +async function login(email, password) { + const user = await User.findOne({ email }); + + if (!user) { + throw new Error("Invalid email or password"); + } + + if (!(await bcrypt.compare(password, user.password))) { + throw new Error("Invalid email or password"); + } + + const token = jwt.sign({ userId: user._id }, secret, { expiresIn: "1h" }); + + const userData = { + success: true, + user: user.toObject(), + token, + }; + + switch (await getUserType(email)) { + case "admin": + userData.admin = await getAdminData(user); + break; + case "client": + userData.client = await getClientData(user); + break; + } + + return userData; +} + +module.exports = login; diff --git a/www/src/@sikka/hajar/core/init.ts b/www/src/@sikka/hajar/core/init.ts index 29da62b..2877a93 100644 --- a/www/src/@sikka/hajar/core/init.ts +++ b/www/src/@sikka/hajar/core/init.ts @@ -68,4 +68,18 @@ export async function getClientData(user: any): Promise { return null; } +<<<<<<< Updated upstream:www/src/@sikka/hajar/core/init.ts export { secret, mongooseInstance, User, Admin, Client }; +======= +module.exports = { + initHajar, + getUserType, + getAdminData, + getClientData, + secret, + mongooseInstance, + User, + Admin, + Client, +}; +>>>>>>> Stashed changes:www/src/@sikka/hajar/core/init.js 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..0d088e3 --- /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); + this.slug = slug; + if (customProperties) { + Object.assign(this, customProperties); + } + } +} + +module.exports = HajarError; diff --git a/www/src/@sikka/hajar/index.js b/www/src/@sikka/hajar/index.js new file mode 100644 index 0000000..b641d3c --- /dev/null +++ b/www/src/@sikka/hajar/index.js @@ -0,0 +1,9 @@ +const { initHajar } = require("./core/init.js"); +const auth = require("./core/auth/index.js"); +const hajar = { + initHajar, + auth, + version: "1.0.41", +}; + +module.exports = hajar;