-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/PrasuHirapara/Hospital-Mana…
- Loading branch information
Showing
61 changed files
with
4,137 additions
and
619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ dist-ssr | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
.env | ||
.vercel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
DB_Url= | ||
PORT= | ||
JWT_SECRET= | ||
NODE_ENV=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.