Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • AF7626/hospital-management-system
1 result
Show changes
Commits on Source (2)
......@@ -17,7 +17,7 @@ const loginAdmin = async (req, res) => {
if (email === process.env.ADMIN_EMAIL && password === process.env.ADMIN_PASSWORD) {
//store the web token in the variable called with JWT secret token
const token = jwt.sign(email+password,process.env.JWT_SECRET)
console.log(token);
//console.log(token);
//send a response back json format
res.json({success:true,token})
}
......@@ -90,8 +90,25 @@ const addDoctor = async (req, res) => {
}
};
// Function to get all a doctor's details
const allDoctors = async (req,res) => {
try {
const doctors = await doctorModel.find({}).select('-password')
res.json({ success: true, doctors })
} catch (error) {
console.log(error)
res.json({ success: false, message: error.message })
}
}
export { addDoctor ,loginAdmin};
\ No newline at end of file
export { addDoctor ,loginAdmin,allDoctors};
\ No newline at end of file
import jwt from 'jsonwebtoken';
//create a function that authenticates the admin using token
const authAdmin = async (req, res, next) => {
// get authorization token
const authorization = req.headers.authorization
//If no token is provided, return an error message
if (!authorization || !authorization.startsWith('Bearer')) {
return res.json({ success: false, message: 'User token missing or invalid!' })
} else {
const AdminToken = authorization.split(' ')[1];
try {
// verity token
const decode_Token = jwt.verify(AdminToken, process.env.JWT_SECRET)
req.decode_Token= decode_Token;
//go to next middler ware
next();
} catch (error) {
console.log(error);
res.json({ success: false, message: error.message })
}
}
}
//export the middleware
export default authAdmin;
import express from 'express';
import { addDoctor ,loginAdmin} from '../controllers/adminController.js';
import { addDoctor, loginAdmin ,allDoctors} from '../controllers/adminController.js';
import authAdmin from '../middlewares/authAdmin.js';
// Create a new router instance for handling admin-related routes
const adminRouter = express.Router();
// This handles POST requests to the endpoint: http://localhost:3000/api/v1/admin/add-doctor
adminRouter.post('/add-doctor', addDoctor);
// Route to login the admin
adminRouter.post('/login', loginAdmin);
// Route to add a new doctor
adminRouter.post('/add-doctor', authAdmin, addDoctor);
//Route to get all doctor
adminRouter.get("/all-doctors", authAdmin, allDoctors)
export default adminRouter;
......@@ -26,9 +26,7 @@ app.use("/api/v1/admin", adminRouter);
app.get("/", (req,res) => {
res.send("Hello its time to ride hello");
})
app.listen(port, () => {
console.log("server is running",port);
......