Skip to content

Commit

Permalink
Merge pull request #57 from sujal1256/main
Browse files Browse the repository at this point in the history
Fixed Patient Signup and Deletion from the database
  • Loading branch information
yazdanhaider authored Oct 23, 2024
2 parents 78ed829 + cccc91e commit c708d86
Show file tree
Hide file tree
Showing 11 changed files with 824 additions and 233 deletions.
2 changes: 1 addition & 1 deletion Backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_Url=
PORT=
JWT_SECRET=
JWT_SECRET=
NODE_ENV=development
59 changes: 42 additions & 17 deletions Backend/api/controllers/PatientController.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
// controllers/patientController.js
import { Patient } from '../models/Patient.js';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { Patient } from "../models/Patient.js";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import { v4 as uuidv4 } from "uuid";

// Register a new patient
export const registerPatient = async (req, res) => {
try {
const { name, email, gender, mobile, password } = req.body;
const { name, email, gender, mobile, age } = req.body;

console.log(req.body);

// Check if the patient already exists
const existingPatient = await Patient.findOne({ email });
if (existingPatient) {
return res.status(400).json({ message: 'Patient already exists' });
return res.status(400).json({ message: "Patient already exists" });
}

// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);

// Create a new patient
const newPatient = new Patient({
const newPatient = await Patient.create({
name,
email,
gender,
mobile,
password: hashedPassword,
age,
patientId: uuidv4(),
});

await newPatient.save();
res.status(201).json({ message: 'Patient registered successfully' });
res
.status(201)
.json({ message: "Patient registered successfully", newPatient });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error', error });
res.status(500).json({ message: "Server error", error });
}
};

export const getAllPatients = async (req, res) => {
const patients = await Patient.find();
return res.status(200).json({ data: patients });
};
// Login a patient
export const loginPatient = async (req, res) => {
try {
Expand All @@ -42,21 +48,40 @@ export const loginPatient = async (req, res) => {
// Find the patient by email
const patient = await Patient.findOne({ email });
if (!patient) {
return res.status(400).json({ message: 'Invalid credentials' });
return res.status(400).json({ message: "Invalid credentials" });
}

// Compare the passwords
const isMatch = await bcrypt.compare(password, patient.password);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid credentials' });
return res.status(400).json({ message: "Invalid credentials" });
}

// Generate a JWT token
const token = jwt.sign({ id: patient._id }, process.env.JWT_SECRET, { expiresIn: '1h' });
const token = jwt.sign({ id: patient._id }, process.env.JWT_SECRET, {
expiresIn: "1h",
});

res.status(200).json({ token });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error', error });
res.status(500).json({ message: "Server error", error });
}
};

export const deletePatient = async (req, res) => {
const { patientId } = req.body;

const patient = await Patient.deleteOne({ _id: patientId });

console.log(patient);

if (!patient) {
return res.status(400).json({
success: false,
message: "Patient not found",
});
}

return res.status();
};
6 changes: 5 additions & 1 deletion Backend/api/models/Patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ const patientSchema = mongoose.Schema({
type: String,
required: true,
},
password: {
patientId: {
type: String,
required: true,
},
age: {
type: Number,
require: true
}
}, { timestamps: true });

export const Patient = mongoose.model('Patient', patientSchema);
6 changes: 5 additions & 1 deletion Backend/api/routes/PatientRoute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// routes/patientRoutes.js
import express from 'express';
import { registerPatient, loginPatient } from '../controllers/PatientController.js';
import { registerPatient, loginPatient, getAllPatients, deletePatient } from '../controllers/PatientController.js';

const router = express.Router();

Expand All @@ -10,4 +10,8 @@ router.post('/register', registerPatient);
// Route for logging in a patient
router.post('/login', loginPatient);

router.get("/get-patients", getAllPatients);

router.post("/delete-patient", deletePatient);

export default router;
16 changes: 15 additions & 1 deletion Backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.9.0",
"mongoose": "^8.7.1",
"nodemon": "^3.1.7"
"nodemon": "^3.1.7",
"uuid": "^10.0.0"
}
}
1 change: 1 addition & 0 deletions Frontend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BASE_URL=
Loading

0 comments on commit c708d86

Please sign in to comment.