mbkauthe 1.0.12 → 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.
Files changed (2) hide show
  1. package/lib/main.js +23 -15
  2. package/package.json +1 -1
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) {
@@ -103,9 +122,7 @@ router.post("/mbkauthe/api/terminateAllSessions", authenticate(mbkautheVar.Main_
103
122
  req.session.destroy((err) => {
104
123
  if (err) {
105
124
  console.log("Error destroying session:", err);
106
- return res
107
- .status(500)
108
- .json({ success: false, message: "Failed to terminate sessions" });
125
+ return res.status(500).json({ success: false, message: "Failed to terminate sessions" });
109
126
  }
110
127
  console.log("All sessions terminated successfully");
111
128
  res.status(200).json({
@@ -171,7 +188,7 @@ router.post("/mbkauthe/api/login", async (req, res) => {
171
188
 
172
189
  if (userResult.rows.length === 0) {
173
190
  console.log(`Username does not exist: ${username}`);
174
- 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" });
175
192
  }
176
193
 
177
194
  const user = userResult.rows[0];
@@ -179,7 +196,7 @@ router.post("/mbkauthe/api/login", async (req, res) => {
179
196
  // Check if the password matches
180
197
  if (user.Password !== password) {
181
198
  console.log(`Incorrect password for username: ${username}`);
182
- return res.status(401).json({ success: false, message: "Incorrect password" });
199
+ return res.status(401).json({ success: false, message: "Incorrect Username Or Password" });
183
200
  }
184
201
 
185
202
  // Check if the account is inactive
@@ -238,18 +255,9 @@ router.post("/mbkauthe/api/login", async (req, res) => {
238
255
  req.session.user = {
239
256
  id: user.id,
240
257
  username: user.UserName,
258
+ role: user.Role,
241
259
  sessionId,
242
260
  };
243
- console.log(`Session stored for user: ${user.UserName}, sessionId: ${sessionId}`); // Log session storage
244
-
245
- // Set a cookie accessible across subDOMAINs
246
- res.cookie("sessionId", sessionId, {
247
- maxAge: COOKIE_EXPIRE_TIME,
248
- DOMAIN: mbkautheVar.IS_DEPLOYED === 'true' ? `.${mbkautheVar.DOMAIN}` : undefined, // Use DOMAIN only in production
249
- httpOnly: true,
250
- secure: mbkautheVar.IS_DEPLOYED === 'true', // Use secure cookies in production
251
- });
252
- console.log(`Cookie set for user: ${user.UserName}, sessionId: ${sessionId}`); // Log cookie setting
253
261
 
254
262
  console.log(`User "${username}" logged in successfully`);
255
263
  res.status(200).json({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mbkauthe",
3
- "version": "1.0.12",
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",