mbkauthe 1.0.18 → 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
@@ -118,13 +126,13 @@ router.post("/mbkauthe/api/terminateAllSessions", authenticate(mbkautheVar.Main_
118
126
  console.log("Error destroying session:", err);
119
127
  return res.status(500).json({ success: false, message: "Failed to terminate sessions" });
120
128
  }
121
-
129
+
122
130
  // Clear all cookies with proper domain
123
131
  const cookieOptions = getCookieOptions();
124
132
  res.clearCookie("mbkauthe.sid", cookieOptions);
125
133
  res.clearCookie("sessionId", cookieOptions);
126
134
  res.clearCookie("username", cookieOptions);
127
-
135
+
128
136
  console.log("All sessions terminated successfully");
129
137
  res.status(200).json({
130
138
  success: true,
@@ -269,10 +277,10 @@ router.post("/mbkauthe/api/logout", async (req, res) => {
269
277
  if (req.session.user) {
270
278
  try {
271
279
  const { id, username } = req.session.user;
272
-
280
+
273
281
  // Clear the SessionId in the database first
274
282
  await dblogin.query(`UPDATE "Users" SET "SessionId" = NULL WHERE "id" = $1`, [id]);
275
-
283
+
276
284
  // Remove the session from the session table
277
285
  if (req.sessionID) {
278
286
  await dblogin.query('DELETE FROM "session" WHERE sid = $1', [req.sessionID]);
@@ -283,13 +291,13 @@ router.post("/mbkauthe/api/logout", async (req, res) => {
283
291
  console.log("Error destroying session:", err);
284
292
  return res.status(500).json({ success: false, message: "Logout failed" });
285
293
  }
286
-
294
+
287
295
  // Clear all cookies with proper domain
288
296
  const cookieOptions = getCookieOptions();
289
297
  res.clearCookie("mbkauthe.sid", cookieOptions);
290
298
  res.clearCookie("sessionId", cookieOptions);
291
299
  res.clearCookie("username", cookieOptions);
292
-
300
+
293
301
  console.log(`User "${username}" logged out successfully`);
294
302
  res.status(200).json({ success: true, message: "Logout successful" });
295
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.18",
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",