@@ -2,7 +2,7 @@ import User from '../models/User.js';
22import bcrypt from 'bcrypt' ;
33import jwt from 'jsonwebtoken' ;
44
5- export const login = async ( req , res ) => {
5+ export const login = async ( req , res , next ) => {
66 try {
77 const { role, email, password } = req . body ;
88
@@ -12,12 +12,16 @@ export const login = async (req, res) => {
1212 } ) ;
1313
1414 if ( ! user ) {
15- return res . status ( 401 ) . json ( { message : 'Invalid role or email' } ) ;
15+ const error = new Error ( "Invalid role or email" ) ;
16+ error . statusCode = 401 ;
17+ return next ( error ) ;
1618 }
1719
1820 const isMatch = await bcrypt . compare ( password , user . password ) ;
1921 if ( ! isMatch ) {
20- return res . status ( 401 ) . json ( { message : 'Invalid password' } ) ;
22+ const error = new Error ( "Invalid credentials" ) ;
23+ error . statusCode = 401 ;
24+ return next ( error ) ;
2125 }
2226
2327 const token = jwt . sign (
@@ -33,19 +37,17 @@ export const login = async (req, res) => {
3337 token,
3438 } ) ;
3539 } catch ( error ) {
36- console . error ( 'Login error:' , error ) ;
37- res . status ( 500 ) . json ( {
38- message : 'Server error' ,
39- error : error . message ,
40- } ) ;
40+ next ( error ) ;
4141 }
4242} ;
4343
44- export const updateDoctorWorkingHours = async ( req , res ) => {
44+ export const updateDoctorWorkingHours = async ( req , res , next ) => {
4545 try {
4646
4747 if ( req . user . role !== 'admin' ) {
48- return res . status ( 403 ) . json ( { message : "Access denied" } ) ;
48+ const error = new Error ( "Access denied" ) ;
49+ error . statusCode = 403 ;
50+ return next ( error ) ;
4951 }
5052
5153 const result = await doctorService . updateWorkingHours (
@@ -56,6 +58,6 @@ export const updateDoctorWorkingHours = async (req, res) => {
5658 res . status ( 200 ) . json ( result ) ;
5759
5860 } catch ( error ) {
59- res . status ( 400 ) . json ( { message : error . message } ) ;
61+ next ( error ) ;
6062 }
6163} ;
0 commit comments