mbkauthe 1.0.11 → 1.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/main.js CHANGED
@@ -63,6 +63,25 @@ const sessionConfig = {
63
63
 
64
64
  router.use(session(sessionConfig));
65
65
 
66
+
67
+ router.use(async (req, res, next) => {
68
+ if (req.session && req.session.user) {
69
+ res.cookie("username", req.session.user.username, {
70
+ maxAge: COOKIE_EXPIRE_TIME,
71
+ path: '/', // Ensure the cookie is available on all paths
72
+ DOMAIN: mbkautheVar.IS_DEPLOYED === 'true' ? `.${mbkautheVar.DOMAIN}` : undefined,
73
+ secure: mbkautheVar.IS_DEPLOYED === 'true',
74
+ });
75
+ res.cookie("sessionId", req.session.user.sessionId, {
76
+ maxAge: COOKIE_EXPIRE_TIME,
77
+ path: '/',
78
+ DOMAIN: mbkautheVar.IS_DEPLOYED === 'true' ? `.${mbkautheVar.DOMAIN}` : undefined,
79
+ secure: mbkautheVar.IS_DEPLOYED === 'true',
80
+ });
81
+ }
82
+ next();
83
+ });
84
+
66
85
  // Middleware to handle session restoration from sessionId cookie
67
86
  router.use(async (req, res, next) => {
68
87
  if (!req.session.user && req.cookies.sessionId) {
@@ -76,6 +95,9 @@ router.use(async (req, res, next) => {
76
95
  req.session.user = {
77
96
  id: user.id,
78
97
  username: user.UserName,
98
+ UserName: user.UserName,
99
+ Role: user.Role,
100
+ role: user.Role,
79
101
  sessionId,
80
102
  };
81
103
  console.log(`Session restored for user: ${user.UserName}`);
@@ -100,9 +122,7 @@ router.post("/mbkauthe/api/terminateAllSessions", authenticate(mbkautheVar.Main_
100
122
  req.session.destroy((err) => {
101
123
  if (err) {
102
124
  console.log("Error destroying session:", err);
103
- return res
104
- .status(500)
105
- .json({ success: false, message: "Failed to terminate sessions" });
125
+ return res.status(500).json({ success: false, message: "Failed to terminate sessions" });
106
126
  }
107
127
  console.log("All sessions terminated successfully");
108
128
  res.status(200).json({
@@ -168,7 +188,7 @@ router.post("/mbkauthe/api/login", async (req, res) => {
168
188
 
169
189
  if (userResult.rows.length === 0) {
170
190
  console.log(`Username does not exist: ${username}`);
171
- return res.status(404).json({ success: false, message: "Username does not exist" });
191
+ return res.status(404).json({ success: false, message: "Incorrect Username Or Password" });
172
192
  }
173
193
 
174
194
  const user = userResult.rows[0];
@@ -176,7 +196,7 @@ router.post("/mbkauthe/api/login", async (req, res) => {
176
196
  // Check if the password matches
177
197
  if (user.Password !== password) {
178
198
  console.log(`Incorrect password for username: ${username}`);
179
- return res.status(401).json({ success: false, message: "Incorrect password" });
199
+ return res.status(401).json({ success: false, message: "Incorrect Username Or Password" });
180
200
  }
181
201
 
182
202
  // Check if the account is inactive
@@ -186,14 +206,12 @@ router.post("/mbkauthe/api/login", async (req, res) => {
186
206
  }
187
207
 
188
208
 
189
- if (mbkautheVar.test === "true") {
190
- // Check if the user is authorized to use the application
191
- if (user.Role !== "SuperAdmin") {
192
- const allowedApps = user.AllowedApps;
193
- if (!allowedApps || !allowedApps.includes(mbkautheVar.APP_NAME)) {
194
- console.warn(`User \"${user.UserName}\" is not authorized to use the application \"${mbkautheVar.APP_NAME}\"`);
195
- return res.status(403).json({ success: false, message: `You Are Not Authorized To Use The Application \"${mbkautheVar.APP_NAME}\"` });
196
- }
209
+ // Check if the user is authorized to use the application
210
+ if (user.Role !== "SuperAdmin") {
211
+ const allowedApps = user.AllowedApps;
212
+ if (!allowedApps || !allowedApps.includes(mbkautheVar.APP_NAME)) {
213
+ console.warn(`User \"${user.UserName}\" is not authorized to use the application \"${mbkautheVar.APP_NAME}\"`);
214
+ return res.status(403).json({ success: false, message: `You Are Not Authorized To Use The Application \"${mbkautheVar.APP_NAME}\"` });
197
215
  }
198
216
  }
199
217
 
@@ -237,18 +255,9 @@ router.post("/mbkauthe/api/login", async (req, res) => {
237
255
  req.session.user = {
238
256
  id: user.id,
239
257
  username: user.UserName,
258
+ role: user.Role,
240
259
  sessionId,
241
260
  };
242
- console.log(`Session stored for user: ${user.UserName}, sessionId: ${sessionId}`); // Log session storage
243
-
244
- // Set a cookie accessible across subDOMAINs
245
- res.cookie("sessionId", sessionId, {
246
- maxAge: COOKIE_EXPIRE_TIME,
247
- DOMAIN: mbkautheVar.IS_DEPLOYED === 'true' ? `.${mbkautheVar.DOMAIN}` : undefined, // Use DOMAIN only in production
248
- httpOnly: true,
249
- secure: mbkautheVar.IS_DEPLOYED === 'true', // Use secure cookies in production
250
- });
251
- console.log(`Cookie set for user: ${user.UserName}, sessionId: ${sessionId}`); // Log cookie setting
252
261
 
253
262
  console.log(`User "${username}" logged in successfully`);
254
263
  res.status(200).json({
@@ -8,7 +8,7 @@ async function validateSession(req, res, next) {
8
8
  const sessionId = req.cookies.sessionId;
9
9
  const query = `SELECT * FROM "Users" WHERE "SessionId" = $1`;
10
10
  const result = await dblogin.query(query, [sessionId]);
11
- const userResult= result.rows[0];
11
+ const userResult = result.rows[0];
12
12
 
13
13
  if (result.rows.length > 0) {
14
14
  const user = result.rows[0];
@@ -32,9 +32,9 @@ async function validateSession(req, res, next) {
32
32
 
33
33
  try {
34
34
  const { id, sessionId } = req.session.user;
35
- const query = `SELECT "SessionId", "Active", "AllowedApps" FROM "Users" WHERE "id" = $1`;
35
+ const query = `SELECT "SessionId", "Active", "Role", "AllowedApps" FROM "Users" WHERE "id" = $1`;
36
36
  const result = await dblogin.query(query, [id]);
37
- const userResult= result.rows[0];
37
+ const userResult = result.rows[0];
38
38
 
39
39
  if (result.rows.length === 0 || userResult.SessionId !== sessionId) {
40
40
  console.log(`Session invalidated for user "${req.session.user.username}"`);
@@ -56,21 +56,16 @@ async function validateSession(req, res, next) {
56
56
  });
57
57
  }
58
58
 
59
- if (mbkautheVar.test === "true") {
60
- if (userResult.Role !== "SuperAdmin") {
61
- const allowedApps = userResult.AllowedApps;
62
- if (!allowedApps || !allowedApps.includes(mbkautheVar.APP_NAME)) {
63
- console.log(`Allowed Apps for user "${req.session.user.username}": ${allowedApps}`);
64
- console.log(!allowedApps);
65
- console.log(!allowedApps.includes(mbkautheVar.APP_NAME));
66
- console.warn(`User \"${req.session.user.username}\" is not authorized to use the application \"${mbkautheVar.APP_NAME}\"`);
67
- req.session.destroy();
68
- res.clearCookie("mbkauthe.sid", { domain: `.${mbkautheVar.DOMAIN}` });
69
- res.clearCookie("sessionId", { domain: `.${mbkautheVar.DOMAIN}` });
70
- return res.render("templates/Error/Error.handlebars", {
71
- error: `You Are Not Authorized To Use The Application \"${mbkautheVar.APP_NAME}\"`,
72
- });
73
- }
59
+ if (userResult.Role !== "SuperAdmin") {
60
+ const allowedApps = userResult.AllowedApps;
61
+ if (!allowedApps || !allowedApps.includes(mbkautheVar.APP_NAME)) {
62
+ console.warn(`User \"${req.session.user.username}\" is not authorized to use the application \"${mbkautheVar.APP_NAME}\"`);
63
+ req.session.destroy();
64
+ res.clearCookie("mbkauthe.sid", { domain: `.${mbkautheVar.DOMAIN}` });
65
+ res.clearCookie("sessionId", { domain: `.${mbkautheVar.DOMAIN}` });
66
+ return res.render("templates/Error/Error.handlebars", {
67
+ error: `You Are Not Authorized To Use The Application \"${mbkautheVar.APP_NAME}\"`,
68
+ });
74
69
  }
75
70
  }
76
71
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mbkauthe",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "MBKTechStudio's reusable authentication system for Node.js applications.",
5
5
  "main": "index.js",
6
6
  "type": "module",