mbkauthe 1.0.17 → 1.0.19

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/index.js CHANGED
@@ -21,7 +21,8 @@ if (mbkautheVar.RECAPTCHA_Enabled === "true") {
21
21
  if (mbkautheVar.RECAPTCHA_SECRET_KEY === undefined) {
22
22
  throw new Error("mbkautheVar.RECAPTCHA_SECRET_KEY is required");
23
23
  }
24
- }
24
+ } console.log(mbkautheVar.IS_DEPLOYED === 'true' ? `.${mbkautheVar.DOMAIN}` : undefined);
25
+
25
26
  if (mbkautheVar.COOKIE_EXPIRE_TIME !== undefined) {
26
27
  const expireTime = parseFloat(mbkautheVar.COOKIE_EXPIRE_TIME);
27
28
  if (isNaN(expireTime) || expireTime <= 0) {
package/lib/main.js CHANGED
@@ -64,6 +64,14 @@ const sessionConfig = {
64
64
  name: 'mbkauthe.sid'
65
65
  };
66
66
 
67
+ router.use(async (req, res, next) => {
68
+ if (req.session && req.session.user) {
69
+ const cookieOptions = getCookieOptions();
70
+ res.cookie("username", req.session.user.username, { ...cookieOptions, httpOnly: false }); // Allow JavaScript access
71
+ res.cookie("sessionId", req.session.user.sessionId, cookieOptions); // Keep httpOnly for sensitive cookies
72
+ }
73
+ next();
74
+ });
67
75
  router.use(session(sessionConfig));
68
76
 
69
77
  // Middleware to handle session restoration from sessionId cookie
@@ -79,12 +87,8 @@ router.use(async (req, res, next) => {
79
87
  req.session.user = {
80
88
  id: user.id,
81
89
  username: user.UserName,
82
- UserName: user.UserName,
83
- Role: user.Role,
84
- role: user.Role,
85
90
  sessionId,
86
91
  };
87
- console.log(`Session restored for user: ${user.UserName}`);
88
92
  }
89
93
  } catch (err) {
90
94
  console.error("Session restoration error:", err);
@@ -122,13 +126,13 @@ router.post("/mbkauthe/api/terminateAllSessions", authenticate(mbkautheVar.Main_
122
126
  console.log("Error destroying session:", err);
123
127
  return res.status(500).json({ success: false, message: "Failed to terminate sessions" });
124
128
  }
125
-
129
+
126
130
  // Clear all cookies with proper domain
127
131
  const cookieOptions = getCookieOptions();
128
132
  res.clearCookie("mbkauthe.sid", cookieOptions);
129
133
  res.clearCookie("sessionId", cookieOptions);
130
134
  res.clearCookie("username", cookieOptions);
131
-
135
+
132
136
  console.log("All sessions terminated successfully");
133
137
  res.status(200).json({
134
138
  success: true,
@@ -273,11 +277,13 @@ router.post("/mbkauthe/api/logout", async (req, res) => {
273
277
  if (req.session.user) {
274
278
  try {
275
279
  const { id, username } = req.session.user;
276
- const query = `SELECT "Active" FROM "Users" WHERE "id" = $1`;
277
- const result = await dblogin.query(query, [id]);
278
280
 
279
- if (result.rows.length > 0 && !result.rows[0].Active) {
280
- console.log("Account is inactive during logout");
281
+ // Clear the SessionId in the database first
282
+ await dblogin.query(`UPDATE "Users" SET "SessionId" = NULL WHERE "id" = $1`, [id]);
283
+
284
+ // Remove the session from the session table
285
+ if (req.sessionID) {
286
+ await dblogin.query('DELETE FROM "session" WHERE sid = $1', [req.sessionID]);
281
287
  }
282
288
 
283
289
  req.session.destroy((err) => {
@@ -285,13 +291,13 @@ router.post("/mbkauthe/api/logout", async (req, res) => {
285
291
  console.log("Error destroying session:", err);
286
292
  return res.status(500).json({ success: false, message: "Logout failed" });
287
293
  }
288
-
294
+
289
295
  // Clear all cookies with proper domain
290
296
  const cookieOptions = getCookieOptions();
291
297
  res.clearCookie("mbkauthe.sid", cookieOptions);
292
298
  res.clearCookie("sessionId", cookieOptions);
293
299
  res.clearCookie("username", cookieOptions);
294
-
300
+
295
301
  console.log(`User "${username}" logged out successfully`);
296
302
  res.status(200).json({ success: true, message: "Logout successful" });
297
303
  });
@@ -3,6 +3,7 @@ const mbkautheVar = JSON.parse(process.env.mbkautheVar);
3
3
 
4
4
  // Get consistent cookie options
5
5
  const getCookieOptions = () => ({
6
+ maxAge: COOKIE_EXPIRE_TIME,
6
7
  domain: mbkautheVar.IS_DEPLOYED === 'true' ? `.${mbkautheVar.DOMAIN}` : undefined,
7
8
  secure: mbkautheVar.IS_DEPLOYED === 'true' ? 'auto' : false,
8
9
  sameSite: 'lax',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mbkauthe",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "description": "MBKTechStudio's reusable authentication system for Node.js applications.",
5
5
  "main": "index.js",
6
6
  "type": "module",