Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
PrasuHirapara committed Oct 16, 2024
2 parents e1af5d9 + f186080 commit b8387be
Show file tree
Hide file tree
Showing 61 changed files with 4,137 additions and 619 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ dist-ssr
*.njsproj
*.sln
*.sw?

.env
.vercel
4 changes: 4 additions & 0 deletions Backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DB_Url=
PORT=
JWT_SECRET=
NODE_ENV=development
62 changes: 62 additions & 0 deletions Backend/api/controllers/PatientController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// controllers/patientController.js
import { Patient } from '../models/Patient.js';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';

// Register a new patient
export const registerPatient = async (req, res) => {
try {
const { name, email, gender, mobile, password } = 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' });
}

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

// Create a new patient
const newPatient = new Patient({
name,
email,
gender,
mobile,
password: hashedPassword,
});

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

// Login a patient
export const loginPatient = async (req, res) => {
try {
const { email, password } = req.body;

// Find the patient by email
const patient = await Patient.findOne({ email });
if (!patient) {
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' });
}

// Generate a JWT token
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 });
}
};
28 changes: 28 additions & 0 deletions Backend/api/models/Patient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// models/Patient.js
import mongoose from 'mongoose';

const patientSchema = mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
gender: {
type: String,
required: true,
},
mobile: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
}, { timestamps: true });

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

const router = express.Router();

// Route for registering a new patient
router.post('/register', registerPatient);

// Route for logging in a patient
router.post('/login', loginPatient);

export default router;
6 changes: 6 additions & 0 deletions Backend/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dotenv from "dotenv";
dotenv.config();

export const PORT = process.env.PORT || 6005;

export const mongoDBUrl = process.env.DB_Url;
Loading

0 comments on commit b8387be

Please sign in to comment.