@@ -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
287419export const updateProfileImage = async ( req , res ) => {
288420 try {
0 commit comments