diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index 13f2e19f..af787c81 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -90,6 +90,34 @@ exports.adminSignUp = async (request, reply) => { // Admin Login Function (With Phone Number) +// exports.adminLogin = async (request, reply) => { +// try { +// const { phone, password } = request.body; + +// // Check if an admin with the phone number exists +// const admin = await Admin.findOne({ phone }); + +// if (!admin) { +// return reply.status(401).send({ message: 'Invalid phone number or password' }); +// } + +// // Compare the password entered by the user with the hashed password stored in the database +// const isPasswordValid = await bcrypt.compare(password, admin.password); + +// if (!isPasswordValid) { +// return reply.status(401).send({ message: 'Invalid phone number or password' }); +// } + + +// // Generate a JWT token for the authenticated admin +// const token = jwt.sign({ phone: admin.phone, role: 'admin' }, JWT_SECRET, { expiresIn: '1h' }); + +// return reply.send({ token, admin }); +// } catch (err) { +// reply.status(500).send({ message: err.message }); +// } +// }; + exports.adminLogin = async (request, reply) => { try { const { phone, password } = request.body; @@ -98,26 +126,59 @@ exports.adminLogin = async (request, reply) => { const admin = await Admin.findOne({ phone }); if (!admin) { - return reply.status(401).send({ message: 'Invalid phone number or password' }); + return reply.status(401).send({ + simplydata: { + error: true, + message: "Invalid phone number or password", + }, + }); } // Compare the password entered by the user with the hashed password stored in the database const isPasswordValid = await bcrypt.compare(password, admin.password); if (!isPasswordValid) { - return reply.status(401).send({ message: 'Invalid phone number or password' }); + return reply.status(401).send({ + simplydata: { + error: true, + message: "Invalid phone number or password", + }, + }); } - // Generate a JWT token for the authenticated admin - const token = jwt.sign({ phone: admin.phone, role: 'admin' }, JWT_SECRET, { expiresIn: '1h' }); + const token = jwt.sign( + { phone: admin.phone, role: admin.role }, + JWT_SECRET, + { expiresIn: "1h" } + ); - return reply.send({ token, admin }); + // Create the response payload + const responsePayload = { + simplydata: { + error: false, + apiversion: process.env.APIVERSION, + access_token: token, + phone: admin.phone, + type: admin.role, + customerId: admin.customerId || null, + username: admin.username || null, + }, + }; + + // Send the response + return reply.send(responsePayload); } catch (err) { - reply.status(500).send({ message: err.message }); + reply.status(500).send({ + simplydata: { + error: true, + message: err.message, + }, + }); } }; + // adminController.js exports.editUserByCustomerId = async (req, reply) => {