Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit b7a248a

Browse files
committed
tt
1 parent 675c117 commit b7a248a

2 files changed

Lines changed: 135 additions & 1 deletion

File tree

server/controllers/integ.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,138 @@ export const external = async (req, res) => {
283283
});
284284
}
285285
};
286+
export const employeeLogin = async (req, res) => {
287+
try {
288+
const { email, password } = req.body;
289+
290+
// Validate input
291+
if (!email || !password) {
292+
return res.status(400).json({
293+
success: false,
294+
message: "Email and password are required"
295+
});
296+
}
297+
298+
// Find user
299+
const user = await User.findOne({ email });
300+
if (!user) {
301+
return res.status(404).json({
302+
success: false,
303+
message: "User not found"
304+
});
305+
}
306+
307+
// Check if user has 'employee' role
308+
if (user.role !== 'employee') {
309+
return res.status(403).json({
310+
success: false,
311+
message: "Access denied. Only employees can use this login endpoint."
312+
});
313+
}
314+
315+
// Check if account is locked
316+
if (user.accountLocked) {
317+
if (user.lockExpiration && user.lockExpiration > new Date()) {
318+
return res.status(403).json({
319+
success: false,
320+
message: "Account is temporarily locked. Please try again later."
321+
});
322+
} else if (user.lockExpiration && user.lockExpiration <= new Date()) {
323+
// Unlock account if lock period has expired
324+
user.accountLocked = false;
325+
user.lockExpiration = null;
326+
await user.save();
327+
} else {
328+
return res.status(403).json({
329+
success: false,
330+
message: "Account is locked. Please contact an administrator."
331+
});
332+
}
333+
}
334+
335+
// Check if account is active
336+
if (!user.isActive) {
337+
return res.status(403).json({
338+
success: false,
339+
message: "Account is deactivated. Please contact an administrator."
340+
});
341+
}
342+
343+
// Compare password
344+
const isPasswordMatch = await bcryptjs.compare(password, user.password);
345+
if (!isPasswordMatch) {
346+
// Check for failed login tracking (optional)
347+
// Implement failed login tracking logic here if needed
348+
349+
return res.status(401).json({
350+
success: false,
351+
message: "Incorrect password"
352+
});
353+
}
354+
355+
// Generate token
356+
const token = jwt.sign(
357+
{
358+
id: user._id,
359+
role: user.role,
360+
department: user.department
361+
},
362+
process.env.JWT_SECRET_KEY,
363+
{ expiresIn: "1h" }
364+
);
365+
366+
// Send Webhook Notification
367+
const webhookUrl = process.env.WEBHOOK_URL;
368+
if (webhookUrl) {
369+
const webhookPayload = {
370+
eventType: "employee_logged_in",
371+
user: {
372+
id: user._id,
373+
email: user.email,
374+
role: user.role,
375+
department: user.department,
376+
},
377+
};
378+
379+
try {
380+
await axios.post(webhookUrl, webhookPayload, {
381+
headers: {
382+
"x-event-type": "employee_logged_in",
383+
"Content-Type": "application/json",
384+
},
385+
});
386+
console.log("Employee login webhook sent successfully.");
387+
} catch (webhookError) {
388+
console.error("Webhook failed:", webhookError.response?.data || webhookError.message);
389+
}
390+
}
391+
392+
// Successful login response
393+
res.status(200).json({
394+
success: true,
395+
message: "Login successful",
396+
token,
397+
user: {
398+
id: user._id,
399+
name: user.name,
400+
username: user.username,
401+
email: user.email,
402+
role: user.role,
403+
department: user.department,
404+
position: user.position,
405+
employeeId: user.employeeId
406+
},
407+
});
408+
409+
} catch (error) {
410+
console.error("Employee Login Error:", error);
411+
res.status(500).json({
412+
success: false,
413+
message: "Internal Server Error",
414+
error: error.message
415+
});
416+
}
417+
};
286418

287419
export const updateProfileImage = async (req, res) => {
288420
try {

server/routes/integ.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
updateProfileImage,
1111
changePasswordSimple,
1212
externaltest,
13-
getHRUsers
13+
getHRUsers,
14+
employeeLogin
1415
} from '../controllers/integ.js'
1516
import { authenticateAdmin } from '../middleware/authMiddleware.js';
1617
import { sendEmployeeComplaint } from '../middleware/employeecomplain.js';
@@ -59,6 +60,7 @@ const router = express.Router()
5960
router.get('/user/:department',authenticateAdmin,getUsersByDepartment)
6061

6162
router.post('/external-login/:department',authenticateAdmin, external);
63+
router.post('/employeelogin',authenticateAdmin, employeeLogin);
6264
router.put('/external-login/:username', authenticateAdmin, upload.single('profileImage'), updateProfileImage);
6365

6466
//test ,checkUserTermsAcceptance

0 commit comments

Comments
 (0)